0

I have this "simple" script I am trying to run with an embedded variable in the path name, I have tried with and without the + and quotes around the whole thing, I cannot seem to get a variable to work in the path. If I run it like this, it outputs to C:\Scripts\Audit instead of to C:\Scripts\Audit\20161229_Audit\GPO_Reports. Can you not have variable in a ScriptBlock, I have seen comments on using an ArgumentList but I am not quite sure how to do this when a variable is to be used within a path name.

$Date = Get-Date -format yyyyMMdd
Invoke-Command -ComputerName localhost -ScriptBlock { cscript "c:\program files\GPMC\Scripts\GetReportsForAllGPOs.wsf" "C:\Scripts\Audit\"+$date+"_Audit\GPO_Reports /domain:contuso.local" } 
1
  • $date -> $using:date Commented Dec 29, 2016 at 15:01

1 Answer 1

1

You have to pass the argumentlist inside the scriptblock since its not in the scope.

Instead Of:

$Date = Get-Date -format yyyyMMdd
Invoke-Command -ComputerName localhost -ScriptBlock { cscript "c:\program files\GPMC\Scripts\GetReportsForAllGPOs.wsf" "C:\Scripts\Audit\"+$date+"_Audit\GPO_Reports /domain:contuso.local" }

DO This:

$Date = Get-Date -format yyyyMMdd
Invoke-Command -ComputerName localhost -ScriptBlock {param($Date) cscript "c:\program files\GPMC\Scripts\GetReportsForAllGPOs.wsf" "C:\Scripts\Audit\$($date)_Audit\GPO_Reports /domain:contuso.local" } -ArgumentList $Date
Sign up to request clarification or add additional context in comments.

5 Comments

That tries to add the /domain:contuso.local into the path name instead of as a parameter but does add the variable correctly. The path 'C:\Scripts\Audit\20161229_Audit\GPO_Reports /domain:contuso.local' could not be found. Verify the path exists.
I see, Invoke-Command -ComputerName localhost -ScriptBlock {param($Date) cscript "c:\program files\GPMC\Scripts\GetReportsForAllGPOs.wsf" "C:\Scripts\Audit\$($date)_Audit\GPO_Reports" " /domain:contuso.local" } -ArgumentList $Date
Yes, I am not considering the code inside the script block. I thought the main issue you had in passing the date inside the block. Why are you adding the /domain: That part could you please cross check because in the complete path it won't accept anything with colon and / , unless its a parameter
@pinchepooch : Ya now its fine. :)
I guess I don't have to, that is an optional argument for the script but is not necessary, I guess it was just ignoring it, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.