PowerShell - drive variable

Dinesh K just a moment. 1 answers, 0 views
powershell

Hello Stackoverflow users,

I am a noob at powershell and this is part of my 1st script I am creating :). I am lost on how I would run a script that is dependent on a drive. I have script that runs task on the d: drive but some hosts does not have a D: drive but has an F: drive instead. What is the best way of adding this variable into the script?

Sample of the script is below

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")

As you can see everything is dependent on D:\Apps.... but in some cases it might be F:\Apps..... How would I put some logic in or variable to know which drive is present? thank you for any help in advance.

Update:

From some help below, I can use the following method for now

$Path = "F:\Apps\NetprobeNT\"
$PathExists = Test-Path $Path
If ($PathExists -eq $True)
{
$DeviceID = "F:"}
Else 
{
$DeviceID = "D:"}

How could I do something similar to the script above that would scan all drives and test-path to determine the $DeviceID? Note - must work for PowerShell 2.0 (windows 2003 host).

Thanks Again.

Update 2 -

I think the best method is the following as it will cater for any drive but I can not get it working. I know I am making a simple mistake -

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:"}
}

I think the following line is the problem

$Path = "$_\Apps\NetprobeNT\"

Any ideas on to get this working?

Thank you

1 Answers


Avshalom 05/06/2016.

You can use WMI Filter, Filter only disk volumes which are not C Drive

$DeviceID = Get-WmiObject win32_logicaldisk -Filter "DriveType=3 AND DeviceID!='C:'" | 
Select DeviceID

Then change the target:

mkdir -Force -Path "$DeviceID\Apps\NetprobeNT\Auto-monitor"

*Also, it's best practice to use one variable and call it each time, more readable, and easier, like this:

$TargetPath = "$DeviceID\Apps\NetprobeNT\Auto-monitor"
mkdir -Force -Path $TargetPath 

Language

Popular Tags