I'm trying to script the automation of creating a file called no_sms_on_drive.sms on all my drives that do not contain sccm, in this case j:. When it starts the Foreach loop I can get the $dltr variable set, but I get an error stating that -Path has a null value.
##Define no_sms_on_drive.sms on all drives except j:\
### Get all the logical disks with drivetype 3 which is a hard drives
$disks = gwmi win32_logicaldisk -Filter "DriveType='3'"
### Filter out j:\ which contains SCCM
$NonSMSDrives = $disks | ? { $_.DeviceID -notmatch "[j]:"}
### Create the no sms file on each of the drives
$MakeNonSMSDrives = ForEach($d in $NonSMSDrives){ $dltr = $_.DeviceID | New-Item -Path $dltr -Name "no_sms_on_drive.sms" -ItemType "File" }
CodePudding user response:
Get-WmiObject
is deprecated. Instead you should use Get-CimInstance
. Since you don't need the variables later on I'd suggest to do it without them.
Get-CimInstance -ClassName CIM_LogicalDisk |
Where-Object {
$_.DriveType -eq 3 -and
$_.DeviceID -notmatch 'j'
} |
ForEach-Object {
$Path = Join-Path -Path $_.DeviceID -ChildPath 'no_sms_on_drive.sms'
New-Item -Path $Path -ItemType 'File' -WhatIf
}
If you're satisfied with the output you can remove the -WhatIf
parameter to actually create the desired files.
CodePudding user response:
I was able to get this working by using the following
$NonSMSDrives = "Get-PSDrive -PSProvider FileSystem | ? { $_.Name -notlike [jdX]} | Select -ExpandProperty Root"
$MakeNonSMSDrives = "ForEach( $d in $NonSMSDrives){ New-Item -Path $d -Name 'no_sms_on_drive.sms' -ItemType 'File' }"