I am trying to write a powershell script that kicks off two workflows.
One needs to be kicked off only once while the other needs to be run on every item in the list. However I am being plagued by an error and I don't know how to get rid of it. When I add a 4 argument I get the same error. I am running the script as an spshelladmin
Error
Exception calling "StartWorkflow" with "3" argument(s): ""
At C:\cert.ps1:26 char: 29
+ $em = $manager.StartWorkFlow <<<< ($items[0],$emails,$data)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Code
$web = Get-SPWeb -Identity "http://portal.com/sites/it"
$manager = $web.Site.WorkFlowManager
$list = $web.Lists["Certificate Tracking"]
$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true
$emails = $list.WorkflowAssociations.GetAssociationByName("Status Update","en-US")
$emails.AllowAsyncManualStart = $true
$emails.AllowManual = $true
$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)
$data = $assoc.AssociationData
$emData = $emails.AssociationData
$count = 0
foreach ($item in $items) {
$wf = $manager.StartWorkFlow($item,$assoc,$data)
}
$em = $manager.StartWorkFlow($items[0],$emails,$emData,$true)
$web.Dispose()
I actually discovered that a specific item I was targeting already had a workflow running on it, I did not place proper checks for the status (check out the comments on this code http://paulryan.com.au/2014/cancel-all-workflows/), very misleading error, I thought I had wrong overload with method call on StartWorkFlow ($item, $association, $data, $true), in SP 2013 there are 4 parameters not sure why the thread title says three... Anyways hope that helps someone.
Related
I've managed to read an OPC XML item with a Powershell script
$wsdl=(Join-path $PSScriptRoot "OpcXmlDa1.00.wsdl")
$global:Proxy = New-WebserviceProxy $wsdl –Namespace X
$global:Options = New-Object X.RequestOptions
$Options.ReturnItemName = $true;
$Options.ReturnItemTime = $true;
$global:ItemList = New-Object X.ReadRequestItemList
$global:RItemList = New-Object X.ReplyItemList
$global:Errors = New-Object X.OPCError
$global:Item = New-Object X.ReadRequestItem
# Read from OPC server
$Proxy.Url = "http://xxx.xx.xx.xxx:xxxx"
$Item.ItemName = "xxx/xxx/xxx"
$ItemList.Items = $Item
$Proxy.Read($Options, $ItemList, [ref]$RItemList, [ref]$Errors)
#Show result
$rTimeStamp = $RItemList.Items[0].Timestamp.ToString('yyyy-MM-dd HH:mm:ss')
$rItemName = $RItemList.Items[0].ItemName
$rValue = $RItemList.Items[0].Value
Write-Host ("{0}: {1}={2}" -f $rTimeStamp, $rItemName, $rValue)
In my result above I see the following in $RItemList.Items[0]
DiagnosticInfo:$null
Value: [UInt32[32]]
Quality: [OPCQuality]
ValueTypeQualifier: $null
ItemPath: ""
ItemName: "xxx/xxx/xxx"
ClientItemHandle: $null
Timestamp: 2020-12 19 01:00:00
TimestampSpecified: $true
ResultID: $null
and when I dive deeper into Value above I see that it is read as expected. So far so good...
Now to my problem; I cannot understand how to write to this same Item via another script.
This is my attempt
$wsdl=(Join-path $PSScriptRoot "OpcXmlDa1.00.wsdl")
$Global:Proxy = New-WebserviceProxy $wsdl –Namespace X
$Global:Options = New-Object X.RequestOptions
$Options.ReturnItemName = $true;
$Options.ReturnItemTime = $true;
$Options.ReturnDiagnosticInfo = $true;
$Global:writeItemList = New-Object X.WriteRequestItemList
$Global:RItemList = New-Object X.ReplyItemList
$Global:Errors = New-Object X.OPCError
$Global:ItemSessionRequest = New-Object X.ItemValue
$ItemSessionRequest.ValueTypeQualifier = 'xsd:unsignedInt' #<- I believe that this might be the issue?
$ItemSessionRequest.ItemPath = ""
$ItemSessionRequest.ItemName = "xxx/xxx/xxx"
$ItemSessionRequest.Value = #($x;$y;$z) #<- I have what I want to write in $x, $y, $z
# Write to OPC server
$Proxy.Url = "http://xxx.xx.xx.xxx:xxxx"
$ReturnValuesOnReply = $true
$writeItemList.Items = $ItemSessionRequest
$Proxy.Write($Options, $writeItemList, $ReturnValuesOnReply, [ref]$RItemList, [ref]$Errors)
I get two errors (I'm running via VSC-code).
Error #1:
DiagnosticInfo : This value to write cannot be NULL
Value :
Quality :
ValueTypeQualifier :
ItemPath :
ItemName : xxx/xxx/SessionRequest
ClientItemHandle :
Timestamp : 1970-01-01 00:59:59
TimestampSpecified : True
ResultID : http://opcfoundation.org/webservices/XMLDA/1.0/:E_BADTYPE
I've assumed that this has to do with my choice of $ItemSessionRequest.ValueTypeQualifier (where I've tried a lot of alternatives).
Error #2: This error comes if I run the script a second time in the same instance
Cannot convert argument "Options", with value: "X.RequestOptions", for "Write" to type "X.RequestOptions": "Cannot convert the "X.RequestOptions" value of type "X.RequestOptions" to type "X.Request
Options"."
At C:\bla.bla.ps1:79 char:1
+ $Proxy.Write($Options, $writeItemList, $ReturnValuesOnReply, [ref]$RI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
I guess this has something to do with the $Proxy variable
I'm grateful for help and I apologize if I missed something relevant, but I have done my best...
In this case problem no 1 was solved by the following
$ItemSessionRequest.Value = #($x;$y;$z)
A work around for problem no 2 was by using a new session inside the script, see 1
I tried to copy the list from root to subsite using PowerShell like the code below, but when in execute there is an error like the picture below. what is missing from my code
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force
$site= new-object Microsoft.SharePoint.SPSite("http://Sharepointroot")
$web = $site.Openweb()
$listroot = $web.Lists["Meeting Room Book"]
$itemsroot = $listroot.items
$siteapps = Get-SPWeb -Identity "http://sharepointroot/Apps"
$listapps = $siteapps.Lists["Meeting Room History"]
$i = 0
foreach ($item in $itemsroot) {
$i++
write-host -foregroundcolor yellow $i
$newitem= $listapps.Items.Add()
$newitem["Title"] = $item["Title"]
$newitem["Booking_x0020_Date"] = $item["Booking_x0020_Date"]
$newitem["End_x0020_Time"] = $item["End_x0020_Time"]
$newitem["Booked_x0020_By"] = $item["Booked_x0020_By"]
$newitem["Display"] = $item["Display"]
$newitem["Category"] = $item["Category"]
$newitem["Status"] = $item["Status"]
$newitem["Recurrence"] = $item["Recurrence"]
$newitem["Meeting_x0020_Title"] = $item["Meeting_x0020_Title"]
$newitem["BB"] = $item["BB"]
$newitem.update()
}
$web.dispose
$site.dispose
start-sleep 10
and I have error like below.
You cannot call a method on a null-valued expression.
at line:5 char:1
+ $newitem= $listapps.Items.Add()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo : InvalidOperation: (:) [],RuntimeException
+FullyUalifiedErrorID : NullArray
Edit:
for this case closed because of an error in me, the name of the list that is made is not the same. thank you for responding to my question.
for code copy list to another subsite its work
To my understanding, this error means the script is attempting to do something, but another part of the script does not have any information to permit the first part of the script to work properly. In this case, $TargetItem .
So I am trying to create a script that will take a print job from one paused print queue and add it to an active queue. However I am trying to utilize the AddJob() function and upon calling it with or without parameters it returns an exception and I am not sure why. Here is what I have so far
$host.Runspace.ThreadOptions = "ReuseThread"
Add-Type -AssemblyName System.Printing
$permissions = [System.Printing.PrintSystemDesiredAccess]::AdministrateServer
$queueperms = [System.Printing.PrintSystemDesiredAccess]::AdministratePrinter
$server = new-object System.Printing.PrintServer -argumentList $permissions
$queues = $server.GetPrintQueues(#([System.Printing.EnumeratedPrintQueueTypes]::Shared))
foreach ($q in $queues) {
if ($q.IsPaused -eq 1)
{
$qPaused = new-object System.Printing.PrintQueue -argumentList $server,$q.Name,1,$queueperms
}
else
{
$qPlaying = new-object System.Printing.PrintQueue -ArgumentList $server,$q.Name,2,$queueperms
}
}
$byteContents = #('This is a test')
$byteContents | Out-File -FilePath "C:\testinput.txt"
[byte[]]$bytes = Get-Content -Encoding byte -Path "C:\testinput.txt"
#$printJob = $qPaused.GetJob(3).
$qPlaying.AddJob()
$jobStream = $printJob.JobStream
$jobStream | Out-GridView
#$jobStream.Write($bytes, 0, $bytes.Length)
#$jobStream.Close()
What this gives me is an error at the $qPlaying.AddJob() saying
Exception calling "AddJob" with "0" argument(s): "Specified argument was out of the range of valid values.
Parameter name: clientPrintSchemaVersion"
At line:23 char:1
+ $qPlaying.AddJob()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentOutOfRangeException
Thank you for any feedback.
The version of the Print Schema is defined when you call the constructor for the queue in this line:
$qPlaying = new-object System.Printing.PrintQueue -ArgumentList $server,$q.Name,2,$queueperms
You are using PrintQueue Constructor (PrintServer, String, Int32, PrintSystemDesiredAccess) where the Int32 is the Print Queue Schema version. The MSDN article has a remark that: "The Print Schema version released with Windows Vista is "1"." Which would make sense that when you use 2 and receive an out of range error that 2 isn't an acceptable value.
You could use 1 as the value or use an alternate constructor. For example:
$qPlaying = new-object System.Printing.PrintQueue -ArgumentList $server,$q.Name,$queueperms
I'm experiencing a problem trying to update a SharePoint User Profile property that is linked to a taxonomy TermSet. The following code results in an exception.
$spsite = Get-SPSite $mysiteurl
$context = Get-SPServiceContext $mysiteurl
$upm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
if ($upm.UserExists($adAccount))
{
$user = $upm.GetUserProfile($adAccount)
$locationsCollection = $user[$propName]
$taxMgr = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($spsite)
$taxStore = $taxMgr.TermStores[0]
$officeLocationsGroup = $taxStore.Groups["Office Locations"]
$officeLocationsTermSet = $officeLocationsGroup.TermSets["Office Locations"]
$terms = $officeLocationsTermSet.GetTerms($newValue, $false)
$foundTerm = $false
$newTerm = $null
foreach ($term in $terms) {
Write-Host " Found: $($term.Name)" -BackgroundColor Yellow -ForegroundColor Black
$foundTerm = $true
$newTerm = $term
}
if (-not $foundTerm) {
$newTerm = $officeLocationsTermSet.CreateTerm($theTermValue, 1033)
$foundTerm = $true
Write-Host " Created: $($newTerm.Name)" -BackgroundColor DarkGreen -ForegroundColor Black
$taxStore.CommitAll()
}
Write-Host "Adding the term..."
$locationsCollection.AddTaxonomyTerm($newTerm)
Write-Host "Term added."
$user.Commit()
Write-Host "Profile Committed."
}
The call to AddTaxonomyTerm triggers this exception:
Exception calling "AddTaxonomyTerm" with "1" argument(s): "Index was out of ran
ge. Must be non-negative and less than the size of the collection.
Parameter name: index"
At E:\mark\updateUserProfile.ps1:41 char:41
+ $locationsCollection.AddTaxonomyTerm <<<< ($newTerm)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Any thoughts on why this would occur would be of great help. Thanks.
It seems that you can't use the AddTaxonomyTerm method on empty fields. You need to "initialize" the taxonomy field first. Use the .Value method first, then the AddTaxonomyTerm method.
$locationsCollection.Value = $newTerm.Name;
$locationsCollection.AddTaxonomyTerm($newTerm)
i am trying to put different header and footer dynamically on each page by dividing into sections
and making linktoprevios false but it gives this error message
Property 'LinkToPrevious' cannot be found on this object; make sure it exists and is settable.
At D:\work\scripts_done\abcd\config.ps1:19 char:36
+ $Doc.LastSection.HeadersFooters. <<<< LinkToPrevious = $false;
+ CategoryInfo : InvalidOperation: (LinkToPrevious:String) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Whats wrong in this ...... my documents takes only 26 line in a page i m trying to inserting header footer on each page
$target_dir="D:\ABCD"
$dir="$target_dir"
$val=ls $dir
$filename="D:\ABCD\er.txt"
$filedata = (get-content $filename)
$Word = New-Object -ComObject Word.Application;
$Word.Visible = $true;
$Doc = $Word.Documents.Add();
$selection.Font.Name="Courier New"
$selection.Font.Size=11
$selection.Font.Spacing=0.5
$selection=$word.Selection
$count=0
foreach ( $line in $filedata ){
if ( $count -eq 26 ){
$Doc.LastSection.HeadersFooters.Header.Add("abcd $count");
$Doc.AddSection();
$Doc.LastSection.HeadersFooters.LinkToPrevious = $false;
$count=0;
}
$count++;
}
AFAICR the LinkToPrevious is not for the section, but for the individual headers/footers. But the HeadersFooters collection is an oddity anyway. I'm thinking in VBA here, not PowerShell, but I'm not sure even your line
$Doc.LastSection.HeadersFooters.Header.Add("abcd $count");
will work. Assuming $Doc.LastSection gives you a Word Section object, I think you need something more like the following, suitably adjusted for PowerShell
$Doc.LastSection.Headers(wdHeaderFooterPrimary).Range.Text = "abcd $count"
$Doc.AddSection()
$Doc.LastSection.Headers(wdHeaderFooterPrimary).LinkToPrevious = $false;