Hello. I am having a hard time using variables in this one line of code.
Originally I was just editing the path for each use, but now I want to use variables to change the Param File.
After & cmd /c is the complete executable path for an installation. This is what I would like to add some variables to.
Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c ‘c:\temp\wfi\disk1\setup.exe -L0x409 -opt c:\temp\wfi\param1.txt’ }
When using Invoke-Command like that, you are running the command in another PowerShell process, and thus it does not know the content of the variable. You can get around that with the ‘Using’ scope modifier. In your case you would change $paramName in your scriptblock to $Using:paramName.
You could use the $using like described above but I believe only if your running PS Version 3 or higher.
I noticed for myself $using sometimes seems a little bit buggy, so I prefer the old school way with the ArgumentList.
$paramName = ‘c:\temp\wfi\param1.txt’
Invoke-Command -ComputerName $computer -ScriptBlock {param($paramName)
& cmd /c 'c:\temp\wfi\disk1\setup.exe -L0x409 -opt ’ $paramName
}-args $paramName
I’m trying to do something very close to the same with my sophos install that has been turned into a powershell script from a batch script.
Here is the part that doesn’t seem to pull the credentials from the variable Password that I have set to have the Administrator type in. I’d prefer that the password pull from an AD field, which I can do, but it doesn’t seem to pass the variable to the cmd.exe part.
I’m trying to do something very close to the same with my sophos install that has been turned into a powershell script from a batch script.
Here is the part that doesn’t seem to pull the credentials from the variable Password that I have set to have the Administrator type in. I’d prefer that the password pull from an AD field, which I can do, but it doesn’t seem to pass the variable to the cmd.exe part.
Please create a new thread for your question so we can keep things tidy here. In your question please also include how you’re defining the $paasword variable, that should help anyone answering your question.
Using -ArgumentList on invoke-command is also my personal preference over $using, but I also like to take a more “PowerShell-y” approach to starting a process:
I changed the names of the variables a bit just to point out that it’s not necessary to have the same variable names inside and outside of the script block, but if course inside the script block you do need to use the same name when naming the parameter in your param declaration as well as when you reference that value in the rest of the code.