查找域中的所有共享

时间:2017-09-28 04:43:59

标签: powershell network-share

是否可以使用PowerShell查找域中的所有网络共享?

我尝试过以下方法:

Get-WmiObject -class Win32_Share -ComputerName test1

1 个答案:

答案 0 :(得分:0)

可以运行一个列出所有共享的脚本。它必须在Windows Server或安装了Microsoft RSAT的计算机上运行。

  1. 安装RSAT(如果需要)。
  2. 在每台目标计算机上,运行winrm quickconfig以允许远程WMI呼叫。
  3. 运行以下powershell脚本。
  4. FindAllShares.ps1

    #This must be run on a computer that has the ActiveDirectory module installed (eg. Windows Server)
    #The module can be installed using the RSAT suite from Microsoft. 
    Import-Module ActiveDirectory
    
    #To connect to remote computers, the following needs to be run on them:
    #winrm quickconfig
    
    #Get all the computers on the domain
    $computers = Get-ADComputer -Filter {enabled -eq $true} | select DNSHostName, Name
    
    $skipComputers = @("COMPUTER1", "COMPUTER2") #This is a list of computers to not check
    $skipShares = @("ADMIN$", "IPC$")
    $allShares = @()
    
    #Loop through all of the computers and ask each for their shares
    foreach ($computer in $computers | sort Name)
    {
        #Write-Host $computer.DNSHostName
    
        if ($skipComputers -contains $computer.Name)
        {
            #skip these computers
        } else
        {
            #Write-Host $computer.Name
    
            #Get the shares on this computer
            $shares = Invoke-Command -Computer $computer.DNSHostName -ScriptBlock {Get-WmiObject -class Win32_Share}
    
            foreach ($share in $shares)
            {
                #Write-Host $share.Name
    
                if ($skipShares -contains $share.Name)
                {
                    #skip these shares
                } else
                {
                    $sharePath = "\\$($computer.Name)\$($share.Name)"
                    #Write-Host $sharePath
    
                    $allShares += $sharePath
                }
            }
        }
    }
    
    #Write-host $($allShares -join ";")
    Write-host $($allShares | Out-String)