簡體   English   中英

Invoke-Command 看不到本地網絡驅動器

[英]Invoke-Command doesn't see local network drives

我有兩台計算機:A 和 B。我正在嘗試自動化一些 CI\CD 任務,我的任務是從 A 遠程啟動 B 上的一些進程。.exe 文件本身位於R驅動器上,這是一個本地網絡驅動器。 所以我這樣做:

# here $cred has encrypted credentials, but it is off topic...
Invoke-Command -ComputerName B -Credential $cred -ScriptBlock {
    R:\WebClient\Platform\UP_110\Proc.exe
}

所以顯然這與在 B 的 PowerShell 上鍵入R:\WebClient\Platform\UP_110\Proc.exe Enter 是一樣的。

現在的問題是在 A 上運行上述代碼時出現此錯誤:

The term 'R:\WebClient\Platform\UP_110\Proc.exe' is not recognized as the name of a cmdlet, function, sc
ript file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is corr
ect and try again.
    + CategoryInfo          : ObjectNotFound: (R:\WebClient\Pl...IMS.UP.Host.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : B

顯然它說我的 B 計算機上沒有R:\WebClient\Platform\UP_110\Proc.exe這樣的文件。 但事實並非如此。 我有它:

在此處輸入圖像描述

事實上,我在 A 和 B 上都有這個 R 驅動器。

如果我將 .exe 移動到C驅動器(對我來說是系統磁盤)下的任何目錄,代碼工作正常,但不適用於R 現在更有趣的是,我可以在 A 和 B 上手動運行R:\WebClient\Platform\UP_110\Proc.exe 它有效。

那么我面臨的問題是什么? 謝謝。

默認情況下,PowerShell Remoting 只能訪問在系統上下文中映射的驅動器。 最常見的是,這將是基於附加硬件的字母驅動器(無論是 USB、SATA、SCSI 等)。

用戶上下文中映射的驅動器(例如遠程驅動器)不會被映射,因為完全登錄的方式與本地登錄不同。 您可以使用兩種解決方法:

  1. 通過 SMB/CIFS 共享訪問文件時使用 UNC 路徑(例如\\server.domain.tld\ShareName\Path\To\Folder\Or\file.ext

  2. 使用New-PSDrive映射傳遞給Invoke-CommandScriptBlock中的驅動器:

     # Single letter drive name New-PSDrive -Name "R" -PSProvider FileSystem -Root "\\server.domain.tld\ShareName" Get-ChildItem R: # More descriptive drive name New-PSDrive -Name "RemoteDrive" -PSProvider FileSystem -Root "\\server.domain.tld\ShareName" Get-ChildItem RemoteDrive:

    需要注意的三點:

    • 上面示例中的Get-ChildItem是為了顯示列出新驅動器的內容應該顯示您希望在遠程目錄中看到的文件。 一旦你確定它對你有用,就可以省略它。

    • 此外,使用較長的驅動器名稱是PowerShell的一項功能,並不意味着您可以從文件資源管理器中將共享文件夾映射為具有多個字符的驅動器。

    • 如果您嘗試使用與啟動Invoke-Command相同的憑據,您可能會遇到嘗試以這種方式映射到遠程驅動器的雙跳問題。 正確解決它超出了 Stack Overflow 的范圍,因為這是 Active Directory 的主要架構考慮因素

      但是,您可以通過構建憑證對象並將其傳遞給
      ScriptBlock中的New-PSDrive ,或使用Invoke-Command運行
      -Authentication CredSSP ,如果您的組織沒有阻止它(很多人阻止它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM