PowerShell - 驱动变量

时间:2016-05-06 10:53:54

标签: powershell

Hello Stackoverflow用户,

我是powershell的菜鸟,这是我创建的第一个脚本的一部分:)。我迷失了如何运行依赖于驱动器的脚本。我有在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\"

有任何关于让这个工作的想法吗?

谢谢

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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