locked
Script to create file in multiple drives RRS feed

  • Question

  • Hello Expert,

    I am in the beginning level of learning power shell scripting....sorry for this bad script published here.

    Wanna to create file in the name no_sms_on_drive.sms on multiple drives of the servers which we targeting the input file.

    $computerlist = Get-Content -Path c:\temp\input.txt
    $namespace = "ROOT\cimv2"
    $classname = "Win32_LogicalDisk"
    
    
    foreach ($computername in $computerlist)
    {
    $overall = Get-WmiObject -Class $classname -ComputerName $computername -Namespace $namespace| Select-Object DeviceID
    
    Foreach ($deviceid in $overall)
    {
     If ($deviceid -eq 'c:')
     {
    New-Item -Path 'C:\no_sms_on_drive.sms' -ItemType File -Force
    }
    Foreach ($deviceid1 in $overall)
    {
     If ($deviceid1 -eq 'D:')
     {
    New-Item -Path 'D:\no_sms_on_drive.sms' -ItemType File -Force
    }
    }2
    Foreach ($deviceid in $overall)
    {
     If ($deviceid2 -eq 'E:')
     {
    New-Item -Path 'E:\no_sms_on_drive.sms' -ItemType File -Force
    }
    }
    
    }}

    But when i execute the script it will get the last drive(G:) as $deviceid,$deviceid1,$deviceid2 from the list of drives and creates that file on that drive(G:) only.

    Thanks

    Suresh M


    THANKS SURESH M


    • Edited by Suresh Muniyan Thursday, May 30, 2019 3:21 PM didnt complete my question
    Thursday, May 30, 2019 3:11 PM

Answers

  • Hello All,

    I got few model script from this forum and modified as per my requirement.

    Thats works like charm...

    Thanks all!!!!

    $computerlist = Get-Content -Path c:\temp\input.txt
    
    foreach ($computername in $computerlist)
    {
    $drives = Get-WmiObject Win32_LogicalDisk | Where { $_.DriveType -eq 3 -and $_.DeviceID -ne 'G:' }
    
    ForEach ( $drive in $drives )
     {
      New-Item "$($drive.Name)\no_sms_on_drive.sms" -ItemType File
     }
    } 


    THANKS SURESH M

    Thursday, May 30, 2019 4:28 PM