2016-05-06 16 views
2

こんにちは、StackoverflowのユーザーPowerShell - ドライブ変数

私はパワーシェルでnoobです、これは私が作成している私の最初のスクリプトの一部です:)。私はドライブに依存しているスクリプトをどのように実行するのか迷っています。私はd:ドライブ上でタスクを実行するスクリプトを持っていますが、一部のホストにはD:ドライブはありませんが、代わりにF:ドライブがあります。この変数をスクリプトに追加する最良の方法は何ですか? F \アプリ....場合もあるかもしれません:\アプリ.....どのようにすべてがDに依存している見ることができるように、スクリプトの

サンプル

mkdir -Force -Path D:\Apps\NetprobeNT\Auto-monitor 
Copy-Item D:\Apps\NetprobeNT\selfannounce.xml -Destination D:\Apps\NetprobeNT\Auto-monitor -Force 
$removecomment = Get-Content D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml 
$removecomment = $removecomment -replace "<!--<type>automonitor-windows</type>-->","" -replace "<!-- Autogenerated types -->","" -replace "<!--End of autogenerated types -->","" 
$removecomment | ?{$_.Trim() -ne ""} 
$removecomment | Out-File D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml -Encoding default 


[xml]$selfannounceXml = Get-Content -Path D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml 
$newCommentstart = $selfannounceXml.CreateComment('Autogenerated types') 
$startupNode = $selfannounceXml.netprobe.selfAnnounce.managedEntity.types 
$startupNode.InsertAfter($newCommentstart, $startupNode.LastChild) 
$selfannounceXml.Save("D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml") 

#Get IIS application path 
Import-Module webadministration 
$a = Get-Website | Select-Object Name 
$a | ForEach-Object { 
$_.name = $_.name.replace(" ","") 
} 

#Export file as .txt 
$a | Format-Table -HideTableHeaders | Out-File D:\Apps\NetprobeNT\Auto-monitor\Website.txt 
$b = Get-Content -Path D:\Apps\NetprobeNT\Auto-monitor\Website.txt 
$b | ForEach {$_.TrimEnd()} | ? {$_.trim() -ne '' } > D:\Apps\NetprobeNT\Auto-monitor\Website.txt 
$b = Get-Content -Path D:\Apps\NetprobeNT\Auto-monitor\Website.txt 
@(ForEach ($a in $b) {$a.Replace(' ', '')}) > D:\Apps\NetprobeNT\Auto-monitor\Website.txt 


#Get XML and add IIS path to 'types' 
#Stop-Service -DisplayName NetprobeNT_DES 

[xml]$xmlSA = Get-Content D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml 
$b | ForEach-Object { 
    $tempchild = $xmlSA.CreateElement("type") 
    $tempchild.set_InnerText($_) 
    $newType = $xmlSA.netprobe.selfAnnounce.managedEntity.types.AppendChild($tempchild) 
} 

#$Newcommentstart = 

$xmlSA.Save("D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml") 

[xml]$selfannounceXml = Get-Content -Path D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml 
$newCommentstart = $selfannounceXml.CreateComment('End of Autogenerated types') 
$startupNode = $selfannounceXml.netprobe.selfAnnounce.managedEntity.types 
$startupNode.InsertAfter($newCommentstart, $startupNode.LastChild) 
$selfannounceXml.Save("D:\Apps\NetprobeNT\Auto-monitor\selfannounce.xml") 

を下回っていますどのドライブが存在しているかを知るために、いくつかのロジックを入れたり、変数を入れたりしますか?事前に助けてくれてありがとう。

更新:

以下のいくつかの助けから、私はその上のスクリプトに似た何かができるまで、すべてのドライブおよびテストパスをスキャンする方法を今

$Path = "F:\Apps\NetprobeNT\" 
$PathExists = Test-Path $Path 
If ($PathExists -eq $True) 
{ 
$DeviceID = "F:"} 
Else 
{ 
$DeviceID = "D:"} 

ために、以下の方法を使用することができます$ DeviceIDを決定しますか?注 - PowerShell 2.0(Windows 2003ホスト)で動作する必要があります。

ありがとう。

アップデート2 -

私は最善の方法は、それが任意のドライブに応じるだろうが、私はそれが働いて得ることができないとして、次のだと思います。私は次の行が問題

$Path = "$_\Apps\NetprobeNT\" 

この作業を取得する上の任意のアイデアだと思う

Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND DeviceID!='C:'" | Select DeviceID | Format-Table -HideTableHeaders > c:\DeviceID.txt -Force 
$DeviceID = Get-Content C:\DeviceID.txt 
$DeviceID | ForEach {$_.TrimEnd()} | ? {$_.trim() -ne '' } > c:\DeviceID.txt 

$DeviceID = Get-Content C:\DeviceID.txt 
$Path = "$_\Apps\NetprobeNT\" 
$PathExists = Test-Path $Path 

foreach ($DeviceID in $DeviceID) 
{ 
If ($PathExists -eq $True) 
{ 
$DeviceDrive = $DeviceID} 
Else 
{ 
$DeviceDrive = "C:"} 
} 

- 私は単純なミスを作っています知っていますか?

mkdir -Force -Path "$DeviceID\Apps\NetprobeNT\Auto-monitor" 

*また、それがベストプラクティスです:

はCドライブ

$DeviceID = Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND DeviceID!='C:'" | 
Select DeviceID 

でないだけでディスクボリュームをフィルタリング次にターゲットを変更、あなたはWMIフィルターを使用することができますあなたの

+0

サポートしたいと考えているPowerShellの最小バージョンを指定する価値があります。ケース・イン・ポイント:「Get-Volume」は、最近のバージョンでは利用可能ですが、Windows 7ではデフォルトでは利用できません。 –

+0

こんにちはドン、Windows 2003のホストのために、最も低いバージョンはPowershell 2.0です。ありがとう、 –

答えて

0

私は投稿した別の投稿からこれを達成することができました。以下はデフォルトでC:ドライブ$DeviceDriveです。次に、すべてのボリュームでパスを検索し、trueの場合はドライブを$DeviceDriveに割り当てます。注 - 2つのドライブが同じパスを持つ場合、最後に見つかったドライブを変数に割り当てます。

$DeviceDrive = "C:" 

Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND DeviceID!='C:'" | 
Where-Object { Test-Path "$($_.DeviceID)\Apps\NetprobeNT\" } | 
Foreach-Object { 
    $DeviceDrive = $_.DeviceID 
1

ありがとうございました1つの変数を使用して、次のように毎回読みやすく、簡単に呼び出すことができます。

$TargetPath = "$DeviceID\Apps\NetprobeNT\Auto-monitor" 
mkdir -Force -Path $TargetPath 
+0

こんにちはAvshalom、ありがとう、読みやすい部分に記載されています。私が言及すべきもう一つの問題があります。一部のホストには、1つ以上のボリュームが添付されている場合があります。D :, F:。 Gなどどのように正しい$ DeviceIDを決定できますか?スクリプトを実行するボリュームには\ Apps \ Netprobeフォルダがあります。多くのありがとう –

+0

追加するには、Powershell 2.0を実行している2003ホスト上でエラーが発生していますが、他のバージョンまたはPowershellで動作します。 mkdir -Force -Path "$ DeviceID \ Apps \ NetprobeNT \ Auto-monitor \ test" New-Item:ドライブが見つかりません。 「@ {DeviceID = D」という名前のドライブは存在しません。ラインで :38文字:24 + $ scriptCmd = {&<<<< $ wrappedCmd -Typeディレクトリ@PSBoundParameters} + CategoryInfo:ObjectNotFound:(@ {デバイスID = D:文字列)[新商品]、DriveNotFoundException + FullyQualifiedErrorId:DriveNotFound、Microsoft.PowerShell.Commands.NewItemCommand –

関連する問題