0

PowerShell を使用して、パスが本当に存在しないかどうか、またはパスへのアクセス許可がないかどうかを判断するメカニズムを見つけるのに苦労しています。たとえば、有効なパスで "Test-Path" コマンドレットを使用するが、アクセス許可がない場合、結果は $false になります。try / catch ブロックを使用してコマンドを試行し、 unathorizedAccessException 例外または ItemNotFoundException 例外をキャッチして、それに応じて応答を処理することをお勧めします。

任意の支援をいただければ幸いです。

4

2 に答える 2

1

おそらく、invoke コマンドを使用していて Access Denied が発生する場合は、 を使用する必要がありますCred-SSP

例えば:

$UserName='<username>'
$securekey= ConvertTo-SecureString '<secure-key>' -AsPlainText -Force;
$credentialdetail= New-Object System.Management.Automation.PSCredential -ArgumentList $UserName,$securekey;

Invoke-Command -Credentials $credentialdetail -ComputerName '<computername>' -Authentication CredSSP -ScriptBlock { '<...>' }
于 2016-12-29T10:11:40.943 に答える
0

個人的には、この .net コードを使用して、共有またはローカル パスにアクセスする例外をキャッチします。

このタイプをpowershellに追加します

add-type @"
using System;
using System.IO;

public class CheckFolderAccess {

 public static string HasAccessToWrite(string path)
        {
            try
            {
                using  (FileStream fs = File.Create(Path.Combine(path, "Testing.txt"), 1, FileOptions.DeleteOnClose))
                { }
                return "Allowed";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

}
"@

次のように使用します。

> [checkfolderaccess]::HasAccessToWrite("\\server\c$\system volume information")
Access to path '\\server\c$\system volume information\Testing.txt' denied.


> [checkfolderaccess]::HasAccessToWrite("\\nonexistingserver\nonexistingpath")
Path not found.
于 2012-05-08T05:02:57.117 に答える