【问题标题】:How to use PowerShell to set the time on a remote device?如何使用 PowerShell 在远程设备上设置时间?
【发布时间】:2015-09-28 17:09:14
【问题描述】:

我想将远程设备(运行 Windows IoT 的 Raspberry Pi 2)的日期和时间设置为本地设备的日期时间值。

我创建了一个变量 $dateTime 来保存本地 DateTime。 我将连接远程设备的密码分配给变量 $password。 我创建了一个凭证对象。 我使用 Enter-PSSession 连接到远程设备。 现在我已连接,我尝试使用 Set-Date = $dateTime | 分配远程设备 DateTime外串。

我得到 cannot convertvalue "=" to type "System.TimeSpan" 错误。

$dateTime = Get-Date
$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("myremotedevice     \Administrator",$password)
Enter-PSSession -ComputerName myremotedevice -Credential $cred
Set-Date = $dateTime | Out-String

一旦我通过 PSSession 连接,$dateTime 变量似乎超出了范围。有没有办法解决这个问题?

【问题讨论】:

    标签: powershell raspberry-pi iot windows-10-iot-core


    【解决方案1】:

    我根本不会为此使用 Enter-PSSession,因为那是用于交互式会话。

    我会用这个:

    $dateTime = Get-Date;
    $password = ConvertTo-SecureString "mypassword" -AsPlainText -Force;
    $cred = New-Object System.Management.Automation.PSCredential ("myremotedevice     \Administrator",$password);
    Invoke-Command -ComputerName myremotedevice -Credential $cred -ScriptBlock {
        Set-Date -Date $using:datetime;
    }
    

    或者,如果我有很多事情要执行:

    $dateTime = Get-Date;
    $password = ConvertTo-SecureString "mypassword" -AsPlainText -Force;
    $cred = New-Object System.Management.Automation.PSCredential ("myremotedevice     \Administrator",$password);
    $session = New-PsSession -ComputerName -Credential $cred;
    Invoke-Command -Session $session -ScriptBlock {
        Set-Date -Date $using:datetime;
    }
    Invoke-Command -Session $session -ScriptBlock { [...] }
    .
    .
    Disconnect-PsSession -Session $session;
    

    Passing local variables to a remote session 通常需要using 命名空间。

    【讨论】:

    • 我会试一试并报告。谢谢培根,
    猜你喜欢
    • 2014-10-29
    • 2021-07-02
    • 2013-07-30
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多