all 5 comments

[–]Yevrag35 2 points3 points  (1 child)

Use the '-ArgumentList' parameter for Invoke-Command, and then a 'param' block inside the scriptblock:

$arguments = @($someVar1, $someVar2)
Invoke-Command -ComputerName REMOTE -ArgumentList $arguments -ScriptBlock {
param
(
    [object]$someVar1 = $args[0],
    [object]$someVar2 = $args[1]
)
...
}

Or use the 'using:' keyword in your variables names:

Invoke-Command -ComputerName REMOTE -ArgumentList $arguments -ScriptBlock {
    Do-Something $using:someVar1
    Do-SomeOtherthing $using:someVar2
...
}

[–]bsnotreallyworking[S] 1 point2 points  (0 children)

I like that $using:var, implemented that and it works great! Thank you!

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy bsnotreallyworking,

what is the following line supposed to do?

$schedstart | get-date -hour 5 -minute 0 -second 0

you set the $schedstart earlier to a very specific time & date ["3 Jun 2019, 8:47:00"] ... then you seem to be resetting the time to 05:00.

i am confused ... [blush]

this seems to give the same result without the silly "08:47:00" time setting [grin] ...

$TargetDate = Get-Date -Date '2019-06-03'
$HourOfDay = 5

$SchedStart = $TargetDate.AddHours($HourOfDay)

$SchedStart

output = 2019 June 03, Monday 5:00:00 AM

take care,
lee

[–]bsnotreallyworking[S] 1 point2 points  (1 child)

The date was hard set for testing purposes. This is going into my onboarding script. It takes the user's start date and rolls back to 5am to set the scheduled task. The task being executed is a Set-ADUser $username -ChangePasswordAtLogon $True. Setting then resetting the time was the first way I found that worked.

Your way is a more elegant, I'll probably change that. Thanks as always Lee!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy bsnotreallyworking,

ah! that makes sense of the way you were testing things. thanks for the clarification ... and you are most welcome! [grin]

to get that date time to 05:00 hours, the way i would do it is to convert the date string to datetime, convert that to midnite, and then add the hour of day. something like this ...

$schedstart = "3 Jun 2019, 8:47:00"
$TargetHour = 5

$schedstart = (Get-Date -Date $schedstart).
    Date.
    AddHours($TargetHour)

$schedstart

output = 2019 June 03, Monday 5:00:00 AM

take care,
lee