簡體   English   中英

Powershell-無法運行.ps1,但可以通過控制台本身運行

[英]Powershell - Running .ps1 doesn't work, but running it through the console itself does

在開始之前,請注意我是Powershell的初學者,因此我所提出的一些問題對於經驗豐富的人來說似乎非常明顯和愚蠢。

我的腳本有問題。 如果我將其復制粘貼到Powershell本身,它將正常工作。 但是,將其放在.ps1文件中並使其通過Powershell執行不起作用。 誰能告訴我為什么,以及如何使用.ps1使其工作? 這是代碼:

$Group = import-csv -path C:\Output\Gruppe.csv
$DomainUsers = import-csv -path C:\Output\DomainUsers.csv
$ErrorActionPreference = "SilentlyContinue"
Get-ADGroupMember –identity Test –recursive | select "samaccountname" | Export-csv –path C:\Output\Gruppe.csv -NoTypeInformation
Get-ADUser –Filter * -SearchBase ”ou=Domain Users,dc=sfol,dc=local” | select "samaccountname" | Export-csv –path C:\Output\DomainUsers.csv –NoTypeInformation
Compare-Object $Group $DomainUsers -property samaccountname -IncludeEqual | where-object {$_.SideIndicator -eq "=="} | select "samaccountname" | Export-csv C:\Output\Difference.csv –NoTypeInformation 
(Get-Content C:\Output\Difference.csv) | % {$_ -replace '"', ""} | out-file -FilePath C:\Output\Difference.csv -Force -Encoding ascii
$File = "C:\Output\Difference.csv"
$Time = Get-Date
ForEach ($User in (Get-Content $File))
{   Try {
        Remove-ADGroupMember -Identity "Test" -Member $User -Confirm:$false -ErrorAction Stop
        Add-Content c:\Output\Gruppelog.log -Value "$Time - $User slettet fra gruppen"
    }
    Catch {
        Add-Content c:\Output\Gruppelog.log -Value "$Time - $User medlem kunne ikke blive slettet fra gruppen pga: $($Error[0])"
    }
}

我在撰寫此問題時也注意到了另一個問題。 該腳本的作用是從OU和組中打印出用戶列表。 然后,它使用打印出的兩個文件將OU與組進行比較,並打印出僅包含OU和組中都存在的用戶的新用戶列表。 然后,它使用新的用戶列表從組中刪除用戶(以便OU和組中都沒有用戶)。

第一次運行該腳本時效果很好,但是如果我將用戶重新添加到組中,然后再次運行該腳本,有時它將僅刪除部分用戶。 如果在運行腳本后執行ctrl + c和CLS,則可以正常運行。 如前所述,我是一個初學者,所以我只想知道為什么沒有ctrl + c或cls,它第二次不能100%工作。 抱歉,如果我不善於解釋,並且我不希望您對此提供幫助,因為這不是問題的一部分。 但是,如果可以的話,我將不勝感激。

親切的問候,影子

問題未導入模塊ActiveDirectory。 我認為當您一次導入模塊時,它將留作Powershell以后使用。 事實並非如此,因此我要解決此問題的方法是將Import-Module ActiveDirectory添加到代碼的開頭。 我仍然沒有弄清楚我的獎金問題,因此,如果有人可以提供幫助,那將是很棒的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM