4

数日間答えを探してみましたが、うまくいきませんでした...

ネットワークドライブをチェックし、まだマップされていない場合はマップするpowershell(v3.0)スクリプトがあります。スクリプト自体は特定のポイントまで問題なく動作し、スクリプトの実行中にドライブがマップされてアクセス可能になりますが、スクリプトが終了すると、マップされたドライブが切断されます。ドライブ マッピングを保持する -Persist オプションを使用しています。

PowerShell プロンプトから同じコマンドを実行すると、ドライブがマップされ、PowerShell プロンプトを終了してもマップされたままになります。

以下のサンプルコード

# Drive letter to map to
$destinationDriveLetter = "G"
# Username to map the network drive
$userName = "username"
# Password to use when mapping
$userPass = ConvertTo-SecureString "password" -AsPlainText -Force

#check if Drive is already mapped
if ( Test-Path -LiteralPath $destinationDriveLetter":" -ErrorVariable errVar ) {
    Write-Host "Drive already mapped.`r`n"
    Write-Host "Exiting script`r`n"
    exit
}

Write-Host "Destination Driveletter $destinationDriveLetter not mapped. Doing it...`r`n"

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $userPass
Write-Debug "Command: New-PSDrive -Persist -Name $destinationDriveLetter -PSProvider FileSystem -Root '\\server\share' -Credential $Credential -ErrorVariable errVar"

New-PSDrive -Persist -Name $destinationDriveLetter -PSProvider FileSystem -Root '\\server\share' -Credential $Credential -ErrorVariable errVar

if ( $errVar ) {
    Write-Error "`r`nError mapping destination drive $destinationDriveLetter. Check!`r`n"
}
else {
    Write-Host "Destination drive mapped successfully.`r`n"
    # test if drive truly mapped and destination folder exist
    Test-Path -LiteralPath $destinationDriveLetter":" -ErrorVariable errVar
    # debugging, roar!
    Write-Host "Drives during script:`r`n"
    Get-PSDrive
}
# dirty fix to actually confirm that the drive is mapped during script execution
pause

明らかに、ここでの問題はスクリプトでコマンドを実行することにありますが、これを修正するにはどうすればよいですか? サーバーの再起動後にドライブを自動的にマップする必要があります。私が使用しているソフトウェアは UNC パスを認識しないため、UNC パスを使用しても機能しません。

編集:実行しているOSがWindows Server 2012であることを忘れていました

4

3 に答える 3

1

スクリプトを実行しているユーザー アカウントは? スクリプトは、ドライブを使用するユーザーとして実行する必要があります。Get-Help から:

get-help new-psdrive -parameter Persist
<snipped>
NOTE: Mapped network drives are specific to a user account. Mapped network drives that you create in sessions that 
are started with the "Run as administrator" option or with the credential of another user are not visible in session 
that started without explicit credentials or with the credentials of the current user.
于 2014-06-11T03:38:30.710 に答える
1

私は同じことをしていましたが、私の場合、問題は、powershell コマンド ウィンドウからスクリプトを呼び出していたことです。「Powershellで実行」スクリプトを右クリックしてスクリプトを直接実行した場合、ドライブは消えません。

于 2014-12-03T13:54:09.937 に答える