2016-05-06 48 views
2

您好Stackoverflow用戶,PowerShell - 驅動器變量

我是Powerhell的noob,這是我創建的第一個腳本的一部分:)。我迷失在我將如何運行依賴於驅動器的腳本。我有腳本在d:驅動器上運行任務,但某些主機沒有D:驅動器,但是具有F:驅動器。將這個變量添加到腳本中的最佳方式是什麼?

樣品的腳本低於

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") 

正如你所看到的一切都依賴於d:\ APPS ....但在某些情況下,它可能是F:\ APPS .....如何我會把一些邏輯或變量知道哪個驅動器存在?提前感謝您的任何幫助。

更新:

從下面一些幫助,我可以用下面的方法現在

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

我怎麼會做一些類似上面的腳本會掃描所有驅動器和測試路徑的東西確定$ DeviceID?注 - 必須適用於PowerShell 2.0(Windows 2003主機)。

再次感謝。

更新2 -

我認爲最好的方法如下,因爲它會滿足任何驅動器,但我不能讓它工作。我知道我做一個簡單的錯誤 -

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:"} 
} 

我認爲下面一行是問題

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

上得到這個工作任何想法?

謝謝

+0

可能值得指定您期望支持的最低版本的PowerShell。例如:「Get-Volume」在最新版本中可用,但默認情況下不在Windows 7上。 –

+0

您好,最低版本是Powershell 2.0,因爲Windows 2003主機。謝謝 –

回答

0

我能夠從我發佈的另一篇文章中實現此目的。以下將默認設置$DeviceDrive C:驅動器。然後它將搜索所有卷的路徑,如果爲true,則會將驅動器分配給$DeviceDrive。注意 - 如果兩個驅動器具有相同的路徑,它會將它找到的最後一個驅動器分配給該變量。

$DeviceDrive = "C:" 

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

您可以使用WMI過濾器,過濾器,不C盤只有磁盤卷

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

然後改變目標:

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

*此外,它的最佳實踐使用一個變量並每次調用它,更具可讀性,並且更容易,如下所示:

$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'的驅動器不存在。 At line:38 char:24 + $ scriptCmd = {&<<<< $ wrappedCmd -Type Directory @PSBoundParameters} + CategoryInfo:ObjectNotFound:(@ {DeviceID = D:String)[New-Item],DriveNotFoundException + FullyQualifiedErrorId:DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand –