通过powershell v2

时间:2017-11-22 15:37:16

标签: powershell active-directory scheduled-tasks powershell-v2.0 powershell-v4.0

我有一个脚本,它收集搜索AD环境的预定任务信息。此脚本在Windows Server 2012服务器(Powershell v4)上运行完美,但我无法在Windows Server 2008服务器上运行(运行PS v2)。我提到了PS版本,因为我在2012服务器上执行了脚本,迫使PS执行v2,但它也没有工作。所以,显然,它只适用于PS v4(也许是v3,我不能告诉......但是肯定它不会在PS v2上获胜)。

我需要一些帮助来了解如何修改此脚本以使其与PS v2兼容。提前谢谢。

这里是剧本:

$Computers = (get-adcomputer -filter {operatingsystem -like "*server*"}).name
$ErrorActionPreference = "SilentlyContinue"
$Report = @()
foreach ($Computer in $Computers)
{
    if (test-connection $Computer -quiet -count 1)
    {
        #Computer is online
        $path = "\\" + $Computer + "\c$\Windows\System32\Tasks"
        $tasks = Get-ChildItem -recurse -Path $path -File
        foreach ($task in $tasks)
        {
            $Details = "" | select ComputerName, Task, User, Enabled, Application, Arguments
            $AbsolutePath = $task.directory.fullname + "\" + $task.Name
            $TaskInfo = [xml](Get-Content $AbsolutePath)
            $Details.ComputerName = $Computer
            $Details.Task = $task.name
            $Details.User = $TaskInfo.task.principals.principal.userid
            $Details.Enabled = $TaskInfo.task.settings.enabled
            $Details.Application = $TaskInfo.task.actions.exec.command
            $Details.Arguments = $TaskInfo.task.actions.exec.Arguments

            $Report += $Details
        }
    }
    else
    {
        #Computer is offline
    }
}
$Report |Export-Csv -Path 'c:ScheduledTasks.csv' -NoTypeInformation

我收到此错误:“get-adcomputer”这个术语'不被识别为cmdlet,函数,脚本文件或可操作程序的名称。名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。在C:\ folder \ Search-Tasks-IDs.ps1:1 char:29 + $ Computers =(get-adcomputer<<<< -filter {operatingsystem -like" server"}) .name + CategoryInfo:ObjectNotFound:(get-adcomputer:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException

我有一个新版本可以跳过" get-adcomputer"问题,在预定义列表中搜索服务器名称:

<#
.Synopsis
   PowerShell script to list all Scheduled Tasks and the User ID
.DESCRIPTION
   This script scan the content of the c:\Windows\System32\tasks and search the UserID XML value. 
   The output of the script is a comma-separated log file containing the Computername, Task name, UserID.


Import-Module ActiveDirectory
$VerbosePreference = "continue"
$list = (get-adcomputer -filter {operatingsystem -like "*server*"}).name
#>

$list = Get-Content C:servers.txt
Write-Verbose  -Message "Trying to query $($list.count) servers found in 'servers.txt'"
$ErrorActionPreference = "SilentlyContinue"
$Report = @()

foreach ($server in $list) 
{
    if (Test-Connection -ComputerName $server -Count 1 -Quiet)
    {
        #Computer is online
        $path = "\\" + $server + "\c$\Windows\System32\Tasks"
        $tasks = Get-ChildItem -recurse -Path $path -File
        foreach ($task in $tasks)
        {
            if ($tasks)
            {
                Write-Verbose -Message "I found $($tasks.count) tasks for $server"
            }

            $Details = "" | select ComputerName, Task, User, Enabled, Application, Arguments
            $AbsolutePath = $task.directory.fullname + "\" + $task.Name
            $TaskInfo = [xml](Get-Content $AbsolutePath)
            $Details.ComputerName = $server
            $Details.Task = $task.name
            $Details.User = $TaskInfo.task.principals.principal.userid
            $Details.Enabled = $TaskInfo.task.settings.enabled
            $Details.Application = $TaskInfo.task.actions.exec.command
            $Details.Arguments = $TaskInfo.task.actions.exec.Arguments

            $Report += $Details
        }
    }
    else
    {
        #Computer is offline
    }
}
$Report |Export-Csv -Path 'c:ScheduledTasks.csv' -NoTypeInformation

这个运行并创建csv文件,但它只填充标题(&#34; ComputerName&#34;,&#34; Task&#34;,&#34; User&#34;,&#34;已启用&#34;,&#34;应用程序&#34;,&#34;参数&#34;)和服务器名称。

如果我删除此行$ ErrorActionPreference =&#34; SilentlyContinue&#34;脚本输出显示以下错误:

Get-ChildItem:找不到与参数名称匹配的参数&#39;文件&#39;。在C:\ folder \ Search-Tasks-IDs_new.ps1:25 char:58 + $ tasks = Get-ChildItem -recurse -Path $ path -File&lt;&lt;&lt;&lt; + CategoryInfo:InvalidArgument:(:) [Get-ChildItem],ParameterBindingException + FullyQualifiedErrorId:NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

获取内容:访问路径&#39; C:\&#39;被拒绝。在C:\ folder \ Search-Tasks-IDs_new.ps1:35 char:42 + $ TaskInfo = [xml](Get-Content&lt;&lt;&lt;&lt;&lt; $ AbsolutePath)+ CategoryInfo:PermissionDenied:(C :: String )[Get-Content],UnauthorizedAccessException + FullyQualifiedErrorId:GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

0 个答案:

没有答案