Author Posts

September 15, 2014 at 1:17 pm

heres what i have so far

$computername = 'PC-123456'
$user = 'JDoe'



Invoke-Command  $computername  {

mkdir "\\flileShare\users$\$user\BKUP" -Force
Move-item 'c:\MyFive9.reg' -Destination "\\flileShare\users$\$user\BKUP"
} -Auth CredSSP -Cred $cred 

im looking to pass $user within the Invoke-Command script block

September 15, 2014 at 3:25 pm

You pass arguments via the ArgumentList parameter:

$computername = 'PC-123456'
$user = 'JDoe'
 
Invoke-Command  $computername  {

$user = $args[0]

mkdir "\\flileShare\users$\$user\BKUP" -Force
Move-item 'c:\MyFive9.reg' -Desination "\\flileShare\users$\$user\BKUP"
} -Auth CredSSP -Cred $cred -ArgumentList $user

The script block also allows you to specify named parameters, so you can do this, too:

$computername = 'PC-123456'
$user = 'JDoe'
 
 
 
Invoke-Command  $computername  {
param(
    $user
)

mkdir "\\flileShare\users$\$user\BKUP" -Force
Move-item 'c:\MyFive9.reg' -Desination "\\flileShare\users$\$user\BKUP"
} -Auth CredSSP -Cred $cred -ArgumentList $user

September 15, 2014 at 8:25 pm

You can also use $using:user in v3+.

September 16, 2014 at 3:05 am

Thanks Guys got it working with your help