在Active Directory AD Powershell中再次运行脚本

时间:2019-06-11 08:27:44

标签: powershell

我想问你如何在AD中的所有计算机上运行PowerShell脚本。我尝试将所有计算机名称设置为变量,然后运行脚本foreach,但无法从变量中找出计算机名称到脚本中。

谢谢您的帮助

$HostName  =  @{n="Hostname";e={[System.Net.Dns]::GetHostName()}}
# IP
$IP  =  @{n="IP";e={gwmi Win32_NetworkAdapterConfiguration -filter 'IPEnabled=True'|select -expandproperty IPAddress|Where {$_ -notmatch ':'}}}
#MAC
$MAC  =  @{n="MAC";e={get-wmiobject -class "Win32_NetworkAdapterConfiguration" |Where{$_.IpEnabled -Match "True"} |select MACAddress}}
#OS
$OS  =  @{n="OS";e={(Get-WMIObject win32_operatingsystem).name}}
#UserLogin
$UserProperty = @{n="user";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
#Log ON / Log OFF
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
#TIME
$TimeProeprty = @{n="Time";e={$_.TimeGenerated}}
#SELECT 
Get-EventLog System -Source Microsoft-Windows-Winlogon| Where { $_.TimeGenerated -GT (Get-Date).AddDays(-30) } | select $UserProperty,$TypeProperty,$TimeProeprty,$HostName,$IP,$MAC,$OS    ```

1 个答案:

答案 0 :(得分:0)

将AD中的DNSHostName属性添加到对象吗?

$Computers = Get-ADComputer -Filter * -properties DNSHostName
$HostName  =  @{n="Hostname";e={$computers.DNSHostName}}