PowerShell-驱动变量

PowerShell-驱动变量,powershell,Powershell,各位用户好 我是powershell的noob,这是我正在创建的第一个脚本的一部分:)。我不知道如何运行依赖于驱动器的脚本。我有在d:drive上运行任务的脚本,但有些主机没有d:drive,而是有F:drive。将此变量添加到脚本中的最佳方式是什么 下面是脚本的示例 mkdir -Force -Path D:\Apps\NetprobeNT\Auto-monitor Copy-Item D:\Apps\NetprobeNT\selfannounce.xml -Destination D:\Ap

各位用户好

我是powershell的noob,这是我正在创建的第一个脚本的一部分:)。我不知道如何运行依赖于驱动器的脚本。我有在d:drive上运行任务的脚本,但有些主机没有d:drive,而是有F:drive。将此变量添加到脚本中的最佳方式是什么

下面是脚本的示例

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")
我如何做与上面的脚本类似的事情来扫描所有驱动器和测试路径以确定$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\"
你有什么办法让它工作吗


谢谢

您可以使用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 

我能够从我发布的另一个帖子中实现这一点。默认情况下,以下选项将使
$DeviceDrive
C:drive成为驱动器。然后,它将在所有卷中搜索路径,如果为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

指定您希望支持的PowerShell的最低版本可能是值得的。举个例子:“获取卷”在最新版本中可用,但默认情况下在Windows 7上不可用。您好,由于Windows 2003主机的原因,最低版本是Powershell 2.0。ThanksHi Avshalom,谢谢,在可读性更强的部分注明。还有一个问题我应该提到。某些主机可能连接了1个以上的卷,即D:,F:。G、 等等。如何确定正确的$DeviceID?我要在其上运行脚本的卷将存在\Apps\Netprobe文件夹。多谢添加,我在运行Powershell 2.0的2003主机上遇到以下错误,但适用于其他版本或Powershell mkdir-Force-Path“$DeviceID\Apps\NetprobeNT\Auto monitor\test”新项目:找不到驱动器。名为“@{DeviceID=D”的驱动器不存在。在第38行char:24+$scriptCmd={&
$DeviceDrive = "C:"

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