こんにちは、StackoverflowのユーザーPowerShell - ドライブ変数
私はパワーシェルでnoobです、これは私が作成している私の最初のスクリプトの一部です:)。私はドライブに依存しているスクリプトをどのように実行するのか迷っています。私はd:ドライブ上でタスクを実行するスクリプトを持っていますが、一部のホストにはD:ドライブはありませんが、代わりにF:ドライブがあります。この変数をスクリプトに追加する最良の方法は何ですか? F \アプリ....場合もあるかもしれません:\アプリ.....どのようにすべてがDに依存している見ることができるように、スクリプトの
サンプル
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")
を下回っていますどのドライブが存在しているかを知るために、いくつかのロジックを入れたり、変数を入れたりしますか?事前に助けてくれてありがとう。
更新:
以下のいくつかの助けから、私はその上のスクリプトに似た何かができるまで、すべてのドライブおよびテストパスをスキャンする方法を今
$Path = "F:\Apps\NetprobeNT\"
$PathExists = Test-Path $Path
If ($PathExists -eq $True)
{
$DeviceID = "F:"}
Else
{
$DeviceID = "D:"}
ために、以下の方法を使用することができます$ DeviceIDを決定しますか?注 - PowerShell 2.0(Windows 2003ホスト)で動作する必要があります。
ありがとう。
アップデート2 -
私は最善の方法は、それが任意のドライブに応じるだろうが、私はそれが働いて得ることができないとして、次のだと思います。私は次の行が問題
$Path = "$_\Apps\NetprobeNT\"
この作業を取得する上の任意のアイデアだと思う
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:"}
}
- 私は単純なミスを作っています知っていますか?
mkdir -Force -Path "$DeviceID\Apps\NetprobeNT\Auto-monitor"
*また、それがベストプラクティスです:
はCドライブ
$DeviceID = Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND DeviceID!='C:'" |
Select DeviceID
でないだけでディスクボリュームをフィルタリング次にターゲットを変更、あなたはWMIフィルターを使用することができますあなたの
サポートしたいと考えているPowerShellの最小バージョンを指定する価値があります。ケース・イン・ポイント:「Get-Volume」は、最近のバージョンでは利用可能ですが、Windows 7ではデフォルトでは利用できません。 –
こんにちはドン、Windows 2003のホストのために、最も低いバージョンはPowershell 2.0です。ありがとう、 –