首页 > 代码库 > Powershell 之收集服务器配置
Powershell 之收集服务器配置
function get-srvinfo { <# .SYNOPSIS This is a function to collecting servers WMI®istry information. .DESCRIPTION 1\ parameter -s and -i are optionally, if they have not detected both, script will cover the LocalHost. 2\ parameter -o is optionally, if it‘s not provided, script will use current path to export result. ------Example1------ get information from CTSDC0A and export result to d:\output.csv get-srvinfo -server CTSDC0A -o d:\output.csv <OR> get-srvinfo -s CTSDC0A -o d:\output.csv <OR> get-srvinfo -i serverlist.txt -o d:\output.csv ------Example2------ get information from CTSDC0A,CTSDC0B,CTSWEB0A and export result to d:\output.csv get-srvinfo -server CTSDC0A,CTSDC0B,CTSWEB0A -o d:\output.csv <OR> get-srvinfo -s CTSDC0A,CTSDC0B,CTSWEB0A -o d:\output.csv <OR> get-srvinfo -i serverlist.txt -o d:\output.csv ------Example3------ get localhost information and export result to d:\output.csv get-srvinfo -output d:\output.csv OR get-srvinfo -o d:\output.csv .PARAMETER server Specify the remote computer name. .PARAMETER inputpath Specify a file path to locate server list. .PARAMETER outputpath Specify output CSV file path and name. .EXAMPLE 1\ get information from CTSDC0A and export result to d:\output.csv get-srvinfo -computername CTSDC0A -o d:\output.csv OR get-srvinfo -i serverlist.txt -o d:\output.csv .EXAMPLE 2\ get localhost information and export result to d:\output.csv get-srvinfo -output d:\output.csv OR get-srvinfo -o d:\output.csv #> #Define: parameter Param ( [alias(‘s‘)]$server, [alias(‘i‘)]$inputpath, [alias(‘o‘)]$outputpath ) #Define: empty arrange to save servers for input $serverList=@() #Determine parameter status if($server -eq $null -and $inputpath -eq $null) { $serverList =(gwmi win32_computersystem |select-object name).name Write-Host “INFORMATION: Not detected any input file or servers, script will run for LocalHost [$serverlist]” -ForegroundColor green } elseif($server -ne $null -and $inputpath -ne $null) { Write-Host "ERROR[Parameter]: Please only use one parameter every time for input server list!" -ForegroundColor Red break } elseif($server -eq $null) { $serverList = Get-Content $inputpath } else { $serverList=$server write-host "INFORMATION: Servers [$serverlist] detected!" -ForegroundColor Green } if($outputpath -eq $null) { $crtpath=[string](get-location) $crtdate=(get-date).tostring("yyyy-mm-dd") $outputpath="$crtpath\get-srvinfo-output-$crtdate.csv" Write-Host "INFORMATION: Output path has not detected!" -ForegroundColor Yellow Write-Host "INFORMATION: Export result in [$outputpath]" -ForegroundColor green Write-Host "---------------------------------------------" -ForegroundColor White } else { Write-Host "INFORMATION: Output path has detected!" -ForegroundColor green Write-Host "INFORMATION: Export result in [$outputpath]" -ForegroundColor green Write-Host "---------------------------------------------" -ForegroundColor White } #Process: Collecting information,arrange and export result Foreach ($srv in $serverlist) { #Determine: try to test connectivity status if(test-connection -computername $srv -quiet) { #Determine: try to get WMI & Registry key value from RPC service try { #Collecting: server and os information $baseInfo=gwmi win32_computersystem -computername $srv -ea "stop" $osver=gwmi win32_operatingsystem -computername $srv -ea "stop" #Collecting: SQL installation status $sqlinststatus=(Get-Service -ComputerName $srv -ea "stop"|Select-Object name |Where-Object {$_.name -eq ‘MSSQLSERVER‘}).name #Collecting: network configuration $netConf= gwmi win32_networkadapterconfiguration -namespace "root\cimv2" -computername $srv -ea "stop"|?{$_.ipenabled} #Collecting: persisted route table records $extnetcfg=Gwmi win32_ip4persistedroutetable –computer $srv -ea "stop" #Collecting: registry key value $regObj = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine‘, $srv) $regObjASP= $regobj.OpenSubkey("SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\") $regObjIIS= $regobj.OpenSubkey("SOFTWARE\Microsoft\inetstp\") $regObjUAC= $regobj.OpenSubkey("Software\Microsoft\Windows\CurrentVersion\Policies\System\") $regObjFWdomain= $regObj.opensubkey("SYSTEM\ControlSet001\services\SharedAccess\Parameters\FirewallPolicy\DomainProfile\") $regObjFWpublic= $regObj.opensubkey("SYSTEM\ControlSet001\services\SharedAccess\Parameters\FirewallPolicy\PublicProfile\") $regObjFWstandard= $regObj.opensubkey("SYSTEM\ControlSet001\services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\") #Return: server and os information $rsltServerType=$baseinfo.model $rsltDomainName=$baseinfo.domain $rsltOSversion=$osver.version $rsltSPversion=$osver.servicepackmajorversion #Determine: SP version if ($rsltSPversion -eq 1) { $rsltSPversion="installed" } else { $rsltSPversion="not installed" } #Return: SQL installation status #Determine: SQL installation status $rsltsqlinststatus=$sqlinststatus if ($sqlinststatus -ne $null) { $rsltsqlinststatus="installed" } else { $rsltsqlinststatus="not installed" } #Return: network configuration [string]$rsltIPAddress=$netconf.ipaddress[0] [string]$rsltIPSubnet=$netConf.ipsubnet [string]$rsltDNSSearchorder=$netconf.dnsserversearchorder [string]$rsltNetBIOSstatus=$netconf.tcpipnetbiosoptions [string]$rsltDefaultGW=$netconf.defaultipgateway #Return: persisted route table records [string]$rsltPstnRTrecord=$extnetcfg.destination #Return: registry key value #Determine: registry key for ASP.NET version if($regobjasp -eq $null) { $rsltASPNETregValue="http://www.mamicode.com/N/A" } elseif($regObjasp.GetValue("install") -eq 1) { $rsltASPNETregValue="http://www.mamicode.com/installed" } else { $rsltASPNETregValue="http://www.mamicode.com/not installed" } #Determine: registry key for IIS version if($regobjiis -eq $null) { $rsltIISVerregValue="http://www.mamicode.com/N/A" } else { $rsltIISVerregValue=http://www.mamicode.com/ $regObjiis.GetValue("versionstring") } #Determine: registry key for UAC status if($regobjuac -eq $null) { $rsltUACregValue="http://www.mamicode.com/N/A" } elseif($regObjuac.GetValue("EnableLUA") -eq 0) { $rsltUACregValue="http://www.mamicode.com/Closed" } else { $rsltUACregValue="http://www.mamicode.com/Opened" } #Determine: Firewall status for domain if($regObjFWdomain -eq $null) { $rsltFWdomain="N/A" } elseif($regobjfwdomain.getvalue("enableFirewall") -eq 1) { $rsltFWdomain="Opened" } else { $rsltFWdomain="Closed" } #Determine: Firewall status for public if($regObjFWpublic -eq $null) { $rsltFWpublic="N/A" } elseif($regobjfwpublic.getvalue("enableFirewall") -eq 1) { $rsltFWpublic="Opened" } else { $rsltFWpublic="Closed" } #Determine: Firewall status for standard if($regObjFWstandard -eq $null) { $rsltFWstandard="N/A" } elseif($regObjFWstandard.getvalue("enableFirewall") -eq 1) { $rsltFWstandard="Opened" } else { $rsltFWstandard="Closed" } #Update: total status $rsltTotalStatus="INFO:No issue found" #Print: status for current server Write-Host "[ $srv ]INFO: No issue found" -ForegroundColor Green } catch { #Print: status for current server Write-Host "[ $srv ]ISSUE: RPC unavailable" -ForegroundColor Red #Update: total status and all object properties $rsltTotalStatus="ISSUE: RPC unavailable" $rsltServerType="N/A" $rsltDomainName="N/A" $rsltOSversion="N/A" $rsltSPVersion="N/A" $rsltsqlinststatus="N/A" $rsltASPNETregValue="http://www.mamicode.com/N/A" $rsltIISVerregValue="http://www.mamicode.com/N/A" $rsltUACregValue=http://www.mamicode.com/“N/A”"N/A" $rsltFWpublic="N/A" $rsltFWstandard="N/A" $rsltIPAddress="N/A" $rsltIPSubnet="N/A" $rsltDNSSearchorder="N/A" $rsltNetBIOSstatus="N/A" $rsltDefaultGW="N/A" $rsltPstnRTrecord="N/A" } } else { #Print: status for current server Write-Host "[ $srv ]ISSUE:Connectivity" -ForegroundColor Red #Update: total status and all object properties $rsltTotalStatus="ISSUE:Connectivity" $rsltServerType="N/A" $rsltDomainName="N/A" $rsltOSversion="N/A" $rsltSPVersion="N/A" $rsltsqlinststatus="N/A" $rsltASPNETregValue="http://www.mamicode.com/N/A" $rsltIISVerregValue="http://www.mamicode.com/N/A" $rsltuacregValue="http://www.mamicode.com/N/A" $rsltFWdomain="N/A" $rsltFWpublic="N/A" $rsltFWstandard="N/A" $rsltIPAddress="N/A" $rsltIPSubnet="N/A" $rsltDNSSearchorder="N/A" $rsltNetBIOSstatus="N/A" $rsltDefaultGW="N/A" $rsltPstnRTrecord="N/A" } #Arrange: all information $arrangeInfo = New-Object object Add-Member -MemberType NoteProperty -Name ServerName -Value $srv -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name TotalStatus -Value $rsltTotalStatus -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name ServerType -Value $rsltServerType -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name DomainName -Value $rsltDomainName -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name OSVersion -Value $rsltOSversion -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name SPVersion -Value $rsltSPVersion -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name SQLInstStatus -Value $rsltsqlinststatus -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name ASPNETRegValue -Value $rsltASPNETregValue -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name IISVerRegValue -Value $rsltIISVerregValue -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name UACRegValue -Value $rsltuacregValue -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name FireWall:Domain -Value $rsltFWdomain -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name FireWall:Public -Value $rsltFWpublic -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name FireWall:Standard -Value $rsltFWstandard -InputObject $arrangeInfo #Determine: network ocnfiguration if($netconf.count -eq $null) { Add-Member -MemberType NoteProperty -Name IPAddress[0] -Value "$rsltIPAddress" -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name IPSubnet[0] -Value "$rsltIPSubnet" -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name DNSSearchorder[0] -Value "$rsltDNSSearchorder" -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name NetBIOSstatus[0] -Value $rsltNetBIOSstatus -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name DefaultGW[0] -Value "$rsltDefaultGW" -InputObject $arrangeInfo } else { #Process: loop all the ip address for ($x=0;$x -lt $netconf.count;$x++) { [string]$rsltIPAddress=$netconf[$x].ipaddress[0] [string]$rsltIPSubnet=$netconf[$x].ipsubnet [string]$rsltDefaultGW=$netconf[$x].defaultipgateway [string]$rsltDNSSearchorder=$netconf[$x].dnsserversearchorder [string]$rsltNetBIOSstatus=$netconf[$x].tcpipnetbiosoptions if($rsltDefaultGW -eq $null) { $rsltDefaultGW="N/A" } if($rsltDNSSearchorder -eq $null) { $rsltDNSSearchorder="N/A" } if($rsltNetBIOSstatus -eq $null) { $rsltNetBIOSstatus="N/A" } Add-Member -MemberType NoteProperty -Name IPAddress[$x] -Value $rsltIPAddress -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name IPSubnet[$x] -Value $rsltIPSubnet -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name DefaultGW[$x] -Value $rsltDefaultGW -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name DNSSearchorder[$x] -Value $rsltDNSSearchorder -InputObject $arrangeInfo Add-Member -MemberType NoteProperty -Name NetBIOSstatus[$x] -Value $rsltNetBIOSstatus -InputObject $arrangeInfo } } #Determine: persisted route table records if($extnetcfg.count -eq $null) { Add-Member -MemberType NoteProperty -Name PstnRTrecord[0] -Value $rsltPstnRTrecord -InputObject $arrangeInfo } else { #Process: loop all the persisted route records for($y=0;$y -lt $extnetcfg.count;$y++) { [string]$rsltPstnRTrecord=$extnetcfg[$y].destination Add-Member -MemberType NoteProperty -Name PstnRTrecord[$y] -Value $rsltPstnRTrecord -InputObject $arrangeInfo } } #Arrange: total result $exportinfo += @($arrangeInfo) #Export: total result $exportinfo |Export-Csv $outputpath -notypeinformation } } get-srvinfo
Powershell 之收集服务器配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。