Change date on remote computer

by gb5757870 at 2013-03-05 18:45:40

I want to change the date on a remote Windows server based on input by the user.

I have beginner PS skills and have hobbled together a script which so far does the following:
1. Checks and outputs the the current time of the server
2. Prompts for a new date in the format of ddMMyy
3. Converts that input into a date/time format of dd/mm/yyyy and passes to a variable called $newdate

If I simply run set-date $newdate on the local computer, no problem. However, when running:
Invoke-Command -ComputerName RemoteServer -ScriptBlock {Set-Date $NewDate}, the error “Cannot bind argument to parameter ‘Date’ because it is null” is returned.

I have checked that the value of $NewDate is 15/06/2013

Just to check that the synatx of that command is correct, it works correctly if specifying the actual date:
Invoke-Command -ComputerName RemoteServer -ScriptBlock {Set-Date 15/06/2013}, that works fine.

Pretty stuck at this point and appreciate any pointers
by coderaven at 2013-03-05 20:22:52
If you are merely trying to run the set-date, your invoke-command cannot take a local variable into a remote system that easy, you need to pass the variable as an argument.

Invoke-Command -ComputerName RemoteServer -ScriptBlock {param($NewDate) Set-Date $NewDate} -ArgumentList $DateVar

Let me know if this works or not, set $DateVar to the date you are wanting to set on the remote computer before running this command. If this works do you need the user input etc?
by gb5757870 at 2013-03-06 11:56:53
Thanks, that works exactly as I need it to.

Are you able to able to explain how the structure of that command is passing the variable?
by coderaven at 2013-03-06 16:51:20
The argument list is passed to the script block. Inside the script block, it is nice to use the param() block to take in those parameters so you know their names as the code executes. If you do not use the param() block, you would use the $args variable. To pull argument 1 you would use $args[0] then argument 2 would be $args[1] and so on.

The need to pass the argument is because the script and the $DateVar in the above example run in different spaces. Also, PowerShell 3 introduces some new ways to deal with this depending on your use.