2012-05-07 12 views
0

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

どのような援助をいただければ幸いです。

答えて

0

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

> [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. 
+0

なぜC#はあなたがPowerShellに自分自身でこれを書くことができますか? –

+0

@ShayLevy私はこのQでArismendiとこの悲劇を抱えていました:http://stackoverflow.com/questions/9735449/how-to-verify-whether-the-share-has-write-access/9736379#9736379 C#のバージョンは、書くコードが少なくて済むようになっています。私はパワーシェルの意味論的な中毒ではありません:-)。 –

+0

私はそれで十分です;) –

0

は、この方法でそれを使用する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; 
      } 
     } 

} 
"@ 

をこのタイプを追加

おそらく、あなたが呼び出しコマンドを使用していてAcces 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 { '<...>' } 
関連する問題