I am attempting to add a username to file path so I can create a directory that contains their username.
This is how I want my User Folder to Look:
D:\Test Space\ArtP$
When I do this in a Shell:
PS > $samAccountName = "ArtP"
PS > $user_folder = "D:\Test Space\$samAccountName$"
PS > $user_folder
D:\Test Space\ArtP$
It works as expected. I then try this in a script that is supposed to do this on a remote server:
$samAccountName = "ArtP"
$user_folder = "D:\Test Space\$samAccountName$"
invoke-command -computername remote_server -scriptblock {
new-item -path $user_folder -type directory -Force
}
I get the following error:
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [New-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemComm
and
It is saying my path is null and I believe there is some discrepancy about how the shell interprets quotes and how the script is. How do I get my user name in the file path above properly?
Refer to the section about Passing Local Values in How to pass arguments for remote commands
With Param:
or with $args