2

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

2
  • It might be worth specifying the minimum version of PowerShell you're expecting to support. Case in point: "Get-Volume" is available in recent versions but not on Windows 7 by default. Commented May 6, 2016 at 11:29
  • Hi Don, the lowest version is Powershell 2.0 because of windows 2003 hosts. Thanks Commented May 6, 2016 at 12:44

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Avshalom, Thanks, noted on the more readable part. There is another issue which i should have mentioned. Some hosts may have more than 1 volume attached i.e. D:, F:. G, etc. How can I determine the correct $DeviceID? The volume I want to run the script on will have \Apps\Netprobe folder present. Many thanks
To add i am getting the bellow error on a 2003 host running Powershell 2.0 but works on other versions or Powershell mkdir -Force -Path "$DeviceID\Apps\NetprobeNT\Auto-monitor\test" New-Item : Cannot find drive. A drive with the name '@{DeviceID=D' does not exist. At line:38 char:24 + $scriptCmd = {& <<<< $wrappedCmd -Type Directory @PSBoundParameters } + CategoryInfo : ObjectNotFound: (@{DeviceID=D:String) [New-Item], DriveNotFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewItemCommand
0

I was able to achieve this from another post I posted. The below will make the $DeviceDrive C: drive by default. It will then search all volumes for the path and if true it will assign the drive to $DeviceDrive. Note - if two drives has the same path it will assign the last drive it finds to the variable.

$DeviceDrive = "C:"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.