2016-05-06 27 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 –