PS1文件作为系统运行的问题

时间:2017-02-14 00:49:18

标签: powershell powershell-v5.0

我正在尝试执行使用新的PS 5.1 cmdlet Remove-LocalUser的PS1文件,并且遇到了一些问题。下面是我正在运行的代码块,它将检查Administrator组中的任何本地帐户,如果它们不在$ IgnoreList中则删除它们:

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\BigFix\AdminAccounts\output.txt -append

$users = Get-WmiObject win32_groupuser | Where-Object { $_.GroupComponent -match ‘administrators’ } | ForEach-Object {[wmi]$_.PartComponent }
$IgnoreList = "SOMEACCOUNTNAME"

#Write-host $users.name


:OuterLoop
foreach ($user in $users) {
    foreach ($account in $IgnoreList) {
        if ($user.name -like "$account") {
            continue OuterLoop
        }
    }

    Remove-LocalUser -Name $user.Name
    }
Stop-Transcript

如果我登录到本地管理员帐户,但在通过我们的终端管理控制台部署它作为系统帐户运行时,此脚本在目标计算机上运行良好。

有问题的机器正在运行以下版本的PS:

Name                           Value                                                                                                                                 
----                           -----                                                                                                                                 
PSVersion                      5.1.14409.1005                                                                                                                        
PSEdition                      Desktop                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                               
BuildVersion                   10.0.14409.1005                                                                                                                       
CLRVersion                     4.0.30319.36373                                                                                                                       
WSManStackVersion              3.0                                                                                                                                   
PSRemotingProtocolVersion      2.3                                                                                                                                   
SerializationVersion           1.1.0.1

以下是系统帐户运行时的副本文件的输出:

**********************
Windows PowerShell transcript start
Start time: 20170213172829
Username: **DOMAIN**\SYSTEM
RunAs User: **DOMAIN**\SYSTEM
Machine: QA-G2-CUCNOV-V4 (Microsoft Windows NT 6.1.7601 Service Pack 1)
Host Application: powershell.exe -executionpolicy unrestricted -NonInteractive -NoProfile -file c:\BigFix\AdminAccounts\removeAdmins.ps1
Process ID: 3244
PSVersion: 5.1.14409.1005
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005
BuildVersion: 10.0.14409.1005
CLRVersion: 4.0.30319.36373
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\BigFix\AdminAccounts\output.txt
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Remove-LocalUser : The term 'Remove-LocalUser' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try
again.
At C:\BigFix\AdminAccounts\removeAdmins.ps1:20 char:5
+     Remove-LocalUser -Name $user.Name
+     ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-LocalUser:String) [], Co
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

**********************
Windows PowerShell transcript end
End time: 20170213172829
**********************

对于为什么会发生这种情况的任何想法都将不胜感激!

0 个答案:

没有答案