21

背景:ローカル マシンから次の PowerShell スクリプトを使用して、いくつかのネットワーク ドライブを自動的にマップするとします。

$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("p:", "\\papabox\files");

$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("q:", "\\quebecbox\files");

## problem -- this one does not work because my username/password
## is different on romeobox
$net = $(New-Object -ComObject WScript.Network);
$net.MapNetworkDrive("r:", "\\romeobox\files");

質問: romeobox のユーザー名/パスワードが他の 2 つのボックスのものと異なっていても、romeobox にも接続できるようにスクリプトを変更するにはどうすればよいですか?

4

6 に答える 6

39
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("r:", "\\romeobox\files", $false, "domain\user", "password")

トリックを行う必要があります、

親切、

ダン

于 2009-10-07T10:30:46.867 に答える
14

パスワードをスクリプトまたはデータ ファイルにプレーン テキストで保存せずに保存する方法が必要な場合は、DPAPI を使用してパスワードを保護し、パスワードをファイルに安全に保存して、後でプレーン テキストとして取得できるようにすることができます。

# Stick password into DPAPI storage once - accessible only by current user
Add-Type -assembly System.Security
$passwordBytes = [System.Text.Encoding]::Unicode.GetBytes("Open Sesame")
$entropy = [byte[]](1,2,3,4,5)
$encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect( `
                       $passwordBytes, $entropy, 'CurrentUser')
$encrytpedData | Set-Content -enc byte .\password.bin

# Retrieve and decrypted password
$encrytpedData = Get-Content -enc byte .\password.bin
$unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( `
                       $encrytpedData, $entropy, 'CurrentUser')
$password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData)
$password
于 2009-10-07T15:28:42.680 に答える
12

PowerShell を使用してドライブをマップする方法を探してここに来ましたか?

PowerShell3.0 を使用すると、より簡単な方法があります。 オプションNew-PSDriveで更新されました。-persist例えば

New-PSDrive -Name U -PSProvider FileSystem -Root \\yourserver\your\folder -Credential yourdomain\username -Persist

以前はNew-PSDrive、現在の PowerShell セッションのみに影響がありました。 -persistいわば、マッピングが O/S に登録されます。New-PSDriveを参照

元の質問に答えるために、使用する資格情報を変更できます。を使用-Credentialして domain\username を変更すると、Windows でパスワードの入力を求めるプロンプトが表示されます。もう 1 つの方法は、以下の例のように PSCredential オブジェクトを渡すことです。詳細については、 Get-Credentialを参照してください。

PS C:\> $User = "mydomain\username"
PS C:\> $PWord = ConvertTo-SecureString -String "mypassword" -AsPlainText -Force
PS C:\> $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
PS C:\> New-PSDrive -Name U -PSProvider FileSystem -Root \\domain\some\folder -Credential $Credential -Persist  
于 2012-11-22T12:54:57.993 に答える
2

私の場合、この簡単なワンライナーが機能することがわかりました(「箱から出してすぐに」考えることはほとんどありません;))

(Start-Process -FilePath "C:\windows\system32\NET.exe" -ArgumentList "USE I: \Server\Volume /USER:XXXXX password /persistent:yes" -Wait -Passthru).ExitCode

追加のボーナスとして、報告するための素敵な終了コードを取得します。それが役立つことを願っています。

于 2016-05-17T12:29:13.570 に答える
0

PowerShellネイティブを使用することに設定されている場合(そのようなものがある場合)、次を使用できます:

 New-SMBMapping -LocalPath "V:" -RemotePath $NetworkPath -UserName $UserAndDomain -Password $pass 

参照: https://docs.microsoft.com/en-us/powershell/module/smbshare/new-smbmapping?view=win10-ps

Remove-SMBMapping などの追加のナゲットがいくつかあります。

于 2020-05-20T17:58:52.297 に答える