trying to pass a date from a remote computer to a function in my PS Script. but just gives me junk. from reading i saw that you can not use variables that were declared and intiated inside of a scriptblock on the outside. not sure how to get the date from these PCs. tried to use "$using" and also "$script" but that does not work. get-date works fine directly called to screen
#Fucntion to manipulate the data
Function writeToServer
{
param($server,$Time)
# Data preparation for loading data into SQL table
$InsertResults = @"
INSERT INTO [ServerTimeSync].[dbo].[ServerTimes](SystemName,ShownTimeOnServer)
VALUES ('$SERVER','$Time')
"@
#call the invoke-sqlcmdlet to execute the query
Invoke-sqlcmd @params -Query $InsertResults
}
foreach ($COMPUTER in $COMPUTERS){
icm $COMPUTER -ScriptBlock {$ENV:COMPUTERNAME
$computer
get-date -Format "MM-dd-yyyy hh:mm:ss tt"
$script:sdate = get-date -Format "MM-dd-yyyy hh:mm:ss tt"
}
writeToServer $computer $script:sdate
}