0

The intention is to create a folder on a remote computer C drive.

I'm trying to run this:

$stageSvrs | %{
     Invoke-Command -ComputerName $_ -ScriptBlock { 
         $setupFolder = "c:\Support\test1"
         Write-Host "Creating Support Folder"
         New-Item -Path $setupFolder -type directory -Force 
         Write-Host "Folder creation complete"
     }
}

but I get the following error:

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. 
Provide an argument that is not null or empty, and then try the command again.
At \\company.local\share\share\userdata\username\Documents\WindowsPowerShell\CreateFolder.ps1:2 char:39
+          Invoke-Command -ComputerName $_ -ScriptBlock {
+                                       ~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 3
    So, we don't know what `$stageSvrs` contains. Can you add the code that populates the `$stageSvrs` variable? I guess that's where the root of the error is to be found. – zett42 Sep 02 '22 at 21:33
  • 1
    It looks like you're making this more complicated than it needs to be - if $stageSvrs is a list of valid computer names (strings), Invoke-Command already accepts an array of ComputerNames. You can remove the ForEach-Object (%{ }) and pass $stageSvrs to the -ComputerName parameter directly. As zett42 mentioned, we need to what is in $stageSvrs - that is likely where the problem is. – Joshua McKinnon Sep 02 '22 at 21:39

0 Answers0