none
Need to add Import-csv command RRS feed

  • Question

  • Hi Guys,

    Please help me in piping import-csv command in below script.


    import-module activedirectory;
     

    $servers = get-adcomputer "Hostname"  | where {$_.enabled -eq $true};
     

    foreach ($server in $servers) {
     
       
        if(test-connection -cn $server.name -quiet -count 1) {
     
       
        write-host "-------------"; 
        write-host $server.name; 
        write-host "-------------";
     
     
        $shares = gwmi win32_share -computer $server.name;
     
        foreach ($share in $shares) {
     
            
            if (($share.name -ne "print$") -and ($share.path -notlike "*LocalsplOnly*") -and ($share.name -notmatch ".\$") -and ($share.name -ne "ipc$") -and ($share.name -ne "sysvol") -and ($share.name -ne "netlogon")-and ($share.name -ne "admin$")) { 
     
                
                $out = $server.name + "," + $share.name + "," + $share.path;
    $out >> c:\output.csv
                }
     
            }
     
        write-host "";
     
        }
    }

    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:43 PM This is not "fix/debug/rewrite script for me" forum
    Monday, March 18, 2019 6:08 AM

All replies

  • Please post your code correctly using the code posting tool provided.  What you have posted is unreadable.


    \_(ツ)_/

    Monday, March 18, 2019 9:56 AM
  • import-module activedirectory;
     
    
    $servers = get-adcomputer "hostname"  | where {$_.enabled -eq $true};
     
    
    foreach ($server in $servers) {
     
       
        if(test-connection -cn $server.name -quiet -count 1) {
     
       
        write-host "-------------"; 
        write-host $server.name; 
        write-host "-------------";
     
     
        $shares = gwmi win32_share -computer $server.name;
     
        foreach ($share in $shares) {
     
            
            if (($share.name -ne "print$") -and ($share.path -notlike "*LocalsplOnly*") -and ($share.name -notmatch ".\$") -and ($share.name -ne "ipc$") -and ($share.name -ne "sysvol") -and ($share.name -ne "netlogon")-and ($share.name -ne "admin$")) { 
     
                
                $out = $server.name + "," + $share.name + "," + $share.path;
    $out >> c:\output.csv
                }
     
            }
     
        write-host "";
     
        }
    }

    Monday, March 18, 2019 10:01 AM
  • Please edit your original post and post the code.  Please correctly format the code.  Use the Powershell type for the posted code so it is colorized correctly.

    If you copied this code from the Internet you should ask the original author for help changing it. 

    To learn how to use a CmdLet start by reading the help for the CmdLet.

    To learn basic PowerShell start here:

          Microsoft Virtual     Academy - Getting Started with Microsoft PowerShell

          PowerShell Documentation

          PowerShell Style   Guidelines


    \_(ツ)_/

    Monday, March 18, 2019 10:07 AM
  • Hi Guys,

    Please help me in piping import-csv command in below script.


    import-module activedirectory;
     

    $servers = get-adcomputer "Hostname"  | where {$_.enabled -eq $true};
     

    foreach ($server in $servers) {
     
       
        if(test-connection -cn $server.name -quiet -count 1) {
     
       
        write-host "-------------"; 
        write-host $server.name; 
        write-host "-------------";
     
     
        $shares = gwmi win32_share -computer $server.name;
     
        foreach ($share in $shares) {
     
            
            if (($share.name -ne "print$") -and ($share.path -notlike "*LocalsplOnly*") -and ($share.name -notmatch ".\$") -and ($share.name -ne "ipc$") -and ($share.name -ne "sysvol") -and ($share.name -ne "netlogon")-and ($share.name -ne "admin$")) { 
     
                
                $out = $server.name + "," + $share.name + "," + $share.path;
    $out >> c:\output.csv
                }
     
            }
     
        write-host "";
     
        }
    }

    Do you mean, you want to import server names from CSV? 

    Then, here you go.

    $Servers=Import-Csv -Header "Host Name"  -Path <CSV file location>

    Monday, March 18, 2019 11:00 AM