如何在PowerShell中获得本地主机(机器)名称?我使用的是PowerShell 1.0。
类似于Powershell中的bat文件代码
Cmd
wmic path Win32_ComputerSystem get Name
电源外壳
Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
和…
hostname.exe
不是专门针对Powershell 1.0版本,更多的是通过Powershell获取信息的不同可能方法的概述:
Native CmdLet (usually the best, but sadly really slow to get the Computername): (Get-ComputerInfo).CsDNSHostName Returns the "Name of local computer according to the domain name server" (Info about Get-ComputerInfo, Info about its return value) Classical CommandLine executable: hostname.exe Returns the "host name portion of the full computer name of the computer" (Info about hostname.exe Environment Variable: $env:COMPUTERNAME Registry: (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName).ComputerName Static .Net Property: [Environment]::MachineName Returns "the NetBIOS name of this local computer." (Info about the System.Environment class) Static .Net Method: [System.Net.Dns]::GetHostEntry("").HostName Returns the DNS Hostname (fqdn) of the system (Info about the System.Net.Dns.GetHostEntry Method) Property of COM-Object (Computername via deprecated Scripting-Api): (New-Object -ComObject WScript.Network).ComputerName Returns "the name of the computer system." (Info about the WshNetwork Object, Info about the Windows Script Host Objects) Property of WMI-Object: (Get-CimInstance -ClassName Win32_ComputerSystem).Name (Info about Win32_ComputerSystem) P/Invoke a native API function:
Add-Type -TypeDefinition @'
public enum COMPUTER_NAME_FORMAT{
ComputerNameNetBIOS,
ComputerNameDnsHostname,
ComputerNameDnsDomain,
ComputerNameDnsFullyQualified,
ComputerNamePhysicalNetBIOS,
ComputerNamePhysicalDnsHostname,
ComputerNamePhysicalDnsDomain,
ComputerNamePhysicalDnsFullyQualified,
ComputerNameMax,
}
public static class Kernel32{
[System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, System.Text.StringBuilder lpBuffer, ref uint lpnSize);
}
'@
$len = 0
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $null, [ref]$len); #get required size
$sb = [System.Text.StringBuilder]::new([int]$len) #create StringBuilder with required capacity (ensure int constructor is used, otherwise powershell chooses string constructor for uint value...)
$len = $sb.Capacity #get actual capacity of StringBuilder (important, as maybe StringBuilder was constructed differently than expected)
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $sb, [ref]$len);
$sb.ToString()
Returns "The fully qualified DNS name that uniquely identifies the local computer. This name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the cluster virtual server." (Info about the GetComputerNameEx API function, Info about the COMPUTER_NAME_FORMAT enumeration, used c# signature of COMPUTER_NAME_FORMAT (unofficial), used c# signature of GetComputerNameEx (unofficial), short blogpost about P/Invoke in powershell) Note: Use P/Invoke with caution. Sufficiently incorrect usage can actually make powershell crash. The Add-Type call is somewhat slow (but only needs to be called once in the script).
推荐文章
- 如何在PowerShell中获得本地主机名?
- 为什么在Java和。net中不能修改字符串?
- 'throw'和'throw new Exception()'的区别
- c# int到字节[]
- 如何跟踪log4net问题
- foreach vs somlist . foreach (){}
- 为什么try{…}最后{…}好;尝试{…} catch{}坏?
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- _ViewStart的位置和方式。CSHTML布局文件链接?
- PowerShell:如何将数组对象转换为PowerShell中的字符串?
- 新建T()
- 如何将枚举绑定到WPF中的组合框控件?
- 拒绝访问该路径
- Visual Studio - Resx文件默认“内部”为“公共”
- .NET中的KeyDown和KeyPress有什么区别?