获取网络管理员的基本计算机信息

时间:2014-11-21 16:03:10

标签: powershell ftp administration

我将在这个帖子中回答我自己的问题,但请随意给我批评我可以用这个脚本做得更好。我最近不得不在域上的许多计算机上大量安装VNC,并且需要有关其PC的一些信息,例如:

  1. 主机名
  2. 用户名
  3. MAC地址
  4. IPv4地址
  5. 域名
  6. 收集完所有这些信息后,它会被编译成文本文件并上传到FTP服务器,以保存文件不会与我将每个文件命名为用户名称的相同名称冲突。

    这是我第一次使用PowerShell的脚本和第一次体验,所以请告诉我是否有什么可以改进的。

1 个答案:

答案 0 :(得分:0)

function Get-MACAddress {
    ipconfig /all | findstr "Physical" | Where-Object {$_.length -lt 58}
}
function Get-IPAddress {
    ipconfig | findstr "IPv4 Address"
}
function Get-HostName {
   get-content env:computername
}
function Get-UserName{
     get-content env:UserName
}
function Get-DomainName{
get-content env:USERDOMAIN
}
function CreateCSVdocument {
    Get-MACAddress | New-Item C:\Users\$($env:username)\Documents\$($env:username).txt -type file     -force 
    Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Get-IPAddress | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Hostname:"
Get-Hostname | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "User:"
Get-UserName | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt " "

Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt "Domain:"
Get-DomainName | Add-Content C:\Users\$($env:username)\Documents\$($env:username).txt
}
function FTPUpload {
$Dir="C:\Users\$($env:username)\Documents\$($env:username).txt"    

#ftp server 
$ftp = "ftpserver" 
$user = "username" 
$pass = "password"  

$webclient = New-Object System.Net.WebClient 

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  

#list every sql server trace file 
foreach($item in (dir $Dir "$($env:username).txt")){ 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName) 
} 

}
Get-DomainName
Get-IPAddress
Get-MACAddress
Get-HostName
Get-UserName
CreateCSVdocument
FTPUpload