【发布时间】:2019-06-11 19:48:53
【问题描述】:
我想问你如何在 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 ```
【问题讨论】:
-
您可以使用组策略在所有计算机上运行它或使用 Invoke-Command。系统名称保存在环境变量 $Env:COMPUTERNAME
-
@I.T Delinquent 它不起作用 $Computer = (Get-ADComputer -filter *).Name Invoke-Command -ComputerName $computers {
标签: powershell