gpt4 book ai didi

powershell - PowerShell-驱动器变量

转载 作者:行者123 更新时间:2023-12-02 23:58:21 25 4
gpt4 key购买 nike

您好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\"

有什么想法可以使这个工作吗?

谢谢

最佳答案

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

关于powershell - PowerShell-驱动器变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37070582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com