'Can't run Loops or Conditional statements inside Powershell Script on remote computer
I want to run a Powershell script on a remote computer from a docker container. I can't use the WSManConnectionInfo library because of compatibility issues with my base docker image, so I have to use Powershell commands. I am able to run basic commands well. Like:
powershell.AddCommand("invoke-command");
powershell.AddParameter("ComputerName", "computerName");
powershell.AddParameter("Credential", creds);
String testScript = File.ReadAllText("\\Scripts\\TestScript.ps1");
ScriptBlock filter = ScriptBlock.Create(testScript);
powershell.AddParameter("ScriptBlock", (filter));
powershell.AddParameter("ArgumentList", new object[] { "C:\\" });
powershell.Invoke()
The TestScript file looks like this:
param(${destination})
if(condition1){
New-Item -Path ${destination} -Name "TestFolder" -ItemType "directory"}
Now, the script is working fine without the if statement, but it throws the following error when trying with that:
Only a script block that contains exactly one pipeline or command can be converted. Expressions or control structures are not permitted. Verify that the script block contains exactly one pipeline or command.'
Can anyone help me with that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
