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" }
Get-WmiObject
is deprecated. Instead you should useGet-CimInstance
. Since you don't need the variables later on I'd suggest to do it without them.If you're satisfied with the output you can remove the
-WhatIf
parameter to actually create the desired files.