无法在完成时关闭名为PowerShell的窗口

时间:2017-01-19 08:56:31

标签: powershell vbscript

我有一个运行PowerShell脚本的VBScript。 PowerShell脚本完成,但它只显示一个黑色窗口,它不会关闭并返回到VBScript。如果我关闭黑色窗口,脚本将继续。如果自己运行,PowerShell脚本本身会运行并退出。以下是执行PowerShell脚本的我的VBScript:

Set objFSO = CreateObject("Scripting.FileSystemObject")

strUSer = InputBox("Enter the username you wish to test the object for. if there is more than one please put a space between each.")

UserArray = Split(strUser)

For Each x In UserArray
  strMessage = strMessage & x & vbCrLf 
Next

Set objTextFile = objFSO.CreateTextFile("C:\scripts\UserTest.txt", True)
objTextFile.Write (strMessage)
objTextFile.Close

Set objTextFile = Nothing

WScript.Sleep 2000
Set objShell = CreateObject("WScript.Shell")
strChatterCommand = "powershell.exe -ExecutionPolicy Bypass -NoLogo -File C:\scripts\User_Check.ps1"
Set RunChatter = objShell.Exec(strChatterCommand)

strText = ""

Do While Not RunChatter.StdOut.AtEndOfStream
  strText = strText & RunChatter.StdOut.ReadLine() & vbCrLf
Loop

WScript.Echo strText

Set fso = Nothing
Set RunChatter = Nothing

WScript.Quit

Powershell脚本:

#Region Set Variables & Env

$SalesforceCsv = "\\some\path$\Platform_LiveExtract.csv"
$aOutput = @()

#EndRegion Set Variables & Env

#Region Functions

Function Install-ADcmdlets{
    #Ensure Group Policy module is installed
    if ((Get-Module | ? {$_.Name -eq "activedirectory"}) -eq $null){
        $ErrorActionPreference = "stop"
        Try{
            import-module activedirectory
        }Catch{
            Write-Host "Please install the Active Directory powershell cmdlets. Exiting Script..."
            exit
        }   
    }
}

Function get-ChatterCsvDetails ($fCsv, $FedID){

    $Return = ($fCsv | ? {$_.FEDERATIONIDENTIFIER -eq $FedID})

    if (!($Return)){

        Write-Host "No User Account found in Chatter..."

    }else{

        Write-Host "$FedID found in Chatter"
        Write-Host (" - Chatter Account created: " + $Return.CREATEDDATE.subString(0,10))
        Write-Host (" - Chatter Account last logon: " + $Return.LASTLOGINDATE.subString(0,10))

    }
    Return $Return


}

Function Pause-Script{
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    exit
}

Function Get-DomainFromDN ([string]$fDN){

    [string]$fReturn = ""
    [array]$fSplit = $fDN -split ",DC="
    for([int]$i=1; $i -le ($fSplit.count -1); $i++){
        $fReturn += ("." + $fSplit[$i])
    }
    return $fReturn.substring(1,($fReturn.length -1))

}

Function Check-ADObjectChatterReadiness ($fUser, $fCsv){

    $fErrorsFound = $False

    $fUserDomain = (Get-DomainFromDN $fUser.DistinguishedName)
    Write-Host ("Checking object " + $fUserDomain + "\" + ($fUser.Samaccountname))

    #Check Samid cases match FederationID
    if ($fCsv){
        If ($fUser.samaccountname -cne $fCsv.FEDERATIONIDENTIFIER){
            Write-host ("ACTION: Users SamId is in the wrong case. Please change it to " + $fCsv.FEDERATIONIDENTIFIER)
            $fErrorsFound = $True
        }
    }

    #Check first Name not blank
    if (!($fUser.GivenName)){
        Write-host "ACTION: User's GivenName is blank"
        $fErrorsFound = $True
    }

    #Check last name not blank
    if (!($fUser.SN)){
        Write-host "ACTION: User's Surname is blank"
        $fErrorsFound = $True
    }

    #Check DisplayName is not blank
    if (!($fUser.displayName)){
        Write-host "ACTION: User's Display Name is blank"
        $fErrorsFound = $True
    }

    #Check UPN is not blank
    If (!($fUser.userprincipalName)){

        Write-host "ACTION: User's UPN is blank"
        $fErrorsFound = $True

    }else{

        #Check UPN for illegal characters
        if ($fUser.userprincipalName.ToString().contains(" ")){
            Write-host "ACTION: User's UPN contains a space"
            $fErrorsFound = $True
        }

    }

    #Check Email for space
    if ($fUser.Mail){
        if ($fUser.Mail.contains(" ")){
            Write-host "ACTION: User's Email contains a space"
            $fErrorsFound = $True
        }
    }

    #Check User is in the correct OU
    if ($fUserDomain -eq "bskyb.com"){
        if ($fUser.DistinguishedName -notlike "*,OU=User Accounts,DC=bskyb,DC=com"){
            write-host "ACTION: Bskyb User account is not in a supported OU"
            $fErrorsFound = $True
        }
    }else{
        if ($fUser.DistinguishedName -notlike "*,OU=User Accounts,DC=Ext,DC=bskyb,DC=com"){
            write-host "ACTION: Ext User account is not in a supported OU"
            $fErrorsFound = $True
        }
    }

    #If chatter account doesn't exist, check user is a member of federation group
    if (!($fCsv)){
        if ($fUser.extensionattribute2)
        {
            write-host "ACTION: Clear the users ExtensionAttribute2 value.`nAttempting to clear attribute now..."
            try{
                Set-ADUser $fUser -clear Extensionattribute2 -Server $fUserDomain
                Write-Host "ACTION: SUCCESS - Attribute value cleared... please get the user to retest."
            }
            catch{
                Write-Host "ACTION: FAILED... please manually clear the attribute value."
            }
            $fErrorsFound = $True
        }
    }
    else
    {
        if (!($fUser.extensionattribute2))
        {
            write-host "ACTION: Set the users ExtensionAttribute2 value.`nAttempting to set attribute now..."
            try{
                set-aduser $fUser -Add @{extensionAttribute2="ChatterUser"} -Server $fUserDomain
                Write-Host "ACTION: SUCCESS - Attribute value set... please get the user to retest."
            }
            catch{
                Write-Host "ACTION FAILED... please manually set the attribute value."
            }
            $fErrorsFound = $True   
        }
    }

    #Check account is enabled
    If ($fUser.Enabled -eq $false){
        write-host "ACTION: User account is disabled in Active Directory"
        $fErrorsFound = $True
    }

    #Check account is not a member of Deactivated group
    $fUser.MemberOf | % {
        if ($_ -like "*APP-CHATTER-DEACTIVATED*"){
            write-host "ACTION: User has been de-activated from Chatter. Please check with Chatter team."
            $fErrorsFound = $True
        }
    }

    #Check for errors
    if ($fErrorsFound -eq $False){
        Write-host "No issues found with Active Directory object"
    }

}

Function Check-ChatterUser ($fCsv, $fSamid){

    Write-Host `n"--------------------------------------------------"
    Write-Host "------------ Checking account $fSamid ------------"

    $fChatterIssues = ""
    $fLdapFilter = ('(samaccountname=' + $fSamid + ')')
    $fAdCmdletUser = (get-aduser -LDAPFilter $fLdapFilter -server bskyb.com:3268 -Properties givenName,sn,mail,displayName,memberof,ExtensionAttribute2)

    if ($fAdCmdletUser){
        # User/s found

        $oChatterUser = get-ChatterCsvDetails $fCsv $_

        if (($fAdCmdletUser.count) -and ($fAdCmdletUser.count -ge 2)){

            # samid In both domains
            $fAdCmdletUser  | % {       
                Check-ADObjectChatterReadiness $_ $oChatterUser

            }

        }else{

            # 1 User returned   
            Check-ADObjectChatterReadiness $fAdCmdletUser $oChatterUser

        }

    }else{
        #No User found
        Write-Host "No user with samaccoutname found in GC"
    }

    Write-Host "`n------ Complete ------"

}

#EndRegion Functions

#Region Main body

Install-ADcmdlets
$oCsv = Import-Csv $SalesforceCsv


If (!($Users)){

    $users = get-content c:/scripts/ChatterUsers.txt
    $array = $true
}
else
{
    $Users = $User.trim()
}

$users | % {
        Check-ChatterUser $oCsv $_      
}


Do {
    $Users = ""

    If ($Users.trim() -eq ""){
        exit #Script =$true
    }else{
        $users | % {
            Check-ChatterUser $oCsv $_
        }
    }
}until(exit) #Script -eq $true)

#EndRegion Main body

3 个答案:

答案 0 :(得分:0)

你看过powershell.exe的命令行参数吗?

你确定你没有' -noexit' powershell.exe命令行中的参数?

答案 1 :(得分:0)

结果证明这是一个使用.Exec命令的查询。解决方案是运行我创建的包装器脚本以解决问题。

的VBScript

dim objFSO, objShell, StrChatterCommand,strText, objTextFile, strLine
set objFSO = CreateObject("Scripting.FileSystemObject")


Const fsoForWriting = 2

Dim strUser
dim UserArray
Dim strMessage

strUSer = InputBox("Enter the username you wish to test the chatter object for. if there is more than one please put a space between each.")


UserArray = Split(strUser)


for each x in UserArray
  strMessage = strMessage   & x & vbCrLf 
next

Set objTextFile = objFSO.CreateTextFile("c:\scripts\ChatterUsers.txt", true)
objTextFile.Write (strMessage)
    objTextFile.Close


Set objTextFile = Nothing

wscript.sleep 500 

StrChatterCommand = "powershell.exe C:\Scripts\start_script.ps1"
Set objShell = CreateObject("WScript.Shell")
objShell.run strChatterCommand, 1, true


Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\scripts\OutputFile.txt", ForReading)
strText = ""

Do Until objTextFile.AtEndOfStream

    strLine = objTextFile.Readline
    strText = strText & strLine & VbCrLf
Loop

objTextFile.Close

wscript.echo strText


set objFSO = nothing
Set objShell = nothing
Set objTextFile = nothing

wscript.quit

第1版powerhsell脚本:

powershell.exe c:\scripts\WSM_Check_Chatter.ps1 -ExecutionPolicy ByPass  -NoLogo -File | Out-File c:\scripts\OutputFile.txt  -Encoding ASCII -Width 50

2nd powershell脚本:

#Region Set Variables & Env

$SalesforceCsv = "\\some\path$\Platform_LiveExtract.csv"
$aOutput = @()

#EndRegion Set Variables & Env

#Region Functions

Function Install-ADcmdlets{
    #Ensure Group Policy module is installed
    if ((Get-Module | ? {$_.Name -eq "activedirectory"}) -eq $null){
        $ErrorActionPreference = "stop"
        Try{
            import-module activedirectory
        }Catch{
            Write-Host "Please install the Active Directory powershell cmdlets. Exiting Script..."
            exit
        }   
    }
}

Function get-ChatterCsvDetails ($fCsv, $FedID){

    $Return = ($fCsv | ? {$_.FEDERATIONIDENTIFIER -eq $FedID})

    if (!($Return)){

        Write-Host "No User Account found in Chatter..."

    }else{

        Write-Host "$FedID found in Chatter"
        Write-Host (" - Chatter Account created: " + $Return.CREATEDDATE.subString(0,10))
        Write-Host (" - Chatter Account last logon: " + $Return.LASTLOGINDATE.subString(0,10))

    }
    Return $Return


}

Function Pause-Script{
    Write-Host "Press any key to continue ..."
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    exit
}

Function Get-DomainFromDN ([string]$fDN){

    [string]$fReturn = ""
    [array]$fSplit = $fDN -split ",DC="
    for([int]$i=1; $i -le ($fSplit.count -1); $i++){
        $fReturn += ("." + $fSplit[$i])
    }
    return $fReturn.substring(1,($fReturn.length -1))

}

Function Check-ADObjectChatterReadiness ($fUser, $fCsv){

    $fErrorsFound = $False

    $fUserDomain = (Get-DomainFromDN $fUser.DistinguishedName)
    Write-Host ("Checking object " + $fUserDomain + "\" + ($fUser.Samaccountname))

    #Check Samid cases match FederationID
    if ($fCsv){
        If ($fUser.samaccountname -cne $fCsv.FEDERATIONIDENTIFIER){
            Write-host ("ACTION: Users SamId is in the wrong case. Please change it to " + $fCsv.FEDERATIONIDENTIFIER)
            $fErrorsFound = $True
        }
    }

    #Check first Name not blank
    if (!($fUser.GivenName)){
        Write-host "ACTION: User's GivenName is blank"
        $fErrorsFound = $True
    }

    #Check last name not blank
    if (!($fUser.SN)){
        Write-host "ACTION: User's Surname is blank"
        $fErrorsFound = $True
    }

    #Check DisplayName is not blank
    if (!($fUser.displayName)){
        Write-host "ACTION: User's Display Name is blank"
        $fErrorsFound = $True
    }

    #Check UPN is not blank
    If (!($fUser.userprincipalName)){

        Write-host "ACTION: User's UPN is blank"
        $fErrorsFound = $True

    }else{

        #Check UPN for illegal characters
        if ($fUser.userprincipalName.ToString().contains(" ")){
            Write-host "ACTION: User's UPN contains a space"
            $fErrorsFound = $True
        }

    }

    #Check Email for space
    if ($fUser.Mail){
        if ($fUser.Mail.contains(" ")){
            Write-host "ACTION: User's Email contains a space"
            $fErrorsFound = $True
        }
    }

    #Check User is in the correct OU
    if ($fUserDomain -eq "bskyb.com"){
        if ($fUser.DistinguishedName -notlike "*,OU=User Accounts,DC=bskyb,DC=com"){
            write-host "ACTION: Bskyb User account is not in a supported OU"
            $fErrorsFound = $True
        }
    }else{
        if ($fUser.DistinguishedName -notlike "*,OU=User Accounts,DC=Ext,DC=bskyb,DC=com"){
            write-host "ACTION: Ext User account is not in a supported OU"
            $fErrorsFound = $True
        }
    }

    #If chatter account doesn't exist, check user is a member of federation group
    if (!($fCsv)){
        if ($fUser.extensionattribute2)
        {
            write-host "ACTION: Clear the users ExtensionAttribute2 value.`nAttempting to clear attribute now..."
            try{
                Set-ADUser $fUser -clear Extensionattribute2 -Server $fUserDomain
                Write-Host "ACTION: SUCCESS - Attribute value cleared... please get the user to retest."
            }
            catch{
                Write-Host "ACTION: FAILED... please manually clear the attribute value."
            }
            $fErrorsFound = $True
        }
    }
    else
    {
        if (!($fUser.extensionattribute2))
        {
            write-host "ACTION: Set the users ExtensionAttribute2 value.`nAttempting to set attribute now..."
            try{
                set-aduser $fUser -Add @{extensionAttribute2="ChatterUser"} -Server $fUserDomain
                Write-Host "ACTION: SUCCESS - Attribute value set... please get the user to retest."
            }
            catch{
                Write-Host "ACTION FAILED... please manually set the attribute value."
            }
            $fErrorsFound = $True   
        }
    }

    #Check account is enabled
    If ($fUser.Enabled -eq $false){
        write-host "ACTION: User account is disabled in Active Directory"
        $fErrorsFound = $True
    }

    #Check account is not a member of Deactivated group
    $fUser.MemberOf | % {
        if ($_ -like "*APP-CHATTER-DEACTIVATED*"){
            write-host "ACTION: User has been de-activated from Chatter. Please check with Chatter team."
            $fErrorsFound = $True
        }
    }

    #Check for errors
    if ($fErrorsFound -eq $False){
        Write-host "No issues found with Active Directory object"
    }

}

Function Check-ChatterUser ($fCsv, $fSamid){

    Write-Host `n"--------------------------------------------------"
    Write-Host "------------ Checking account $fSamid ------------"

    $fChatterIssues = ""
    $fLdapFilter = ('(samaccountname=' + $fSamid + ')')
    $fAdCmdletUser = (get-aduser -LDAPFilter $fLdapFilter -server bskyb.com:3268 -Properties givenName,sn,mail,displayName,memberof,ExtensionAttribute2)

    if ($fAdCmdletUser){
        # User/s found

        $oChatterUser = get-ChatterCsvDetails $fCsv $_

        if (($fAdCmdletUser.count) -and ($fAdCmdletUser.count -ge 2)){

            # samid In both domains
            $fAdCmdletUser  | % {       
                Check-ADObjectChatterReadiness $_ $oChatterUser

            }

        }else{

            # 1 User returned   
            Check-ADObjectChatterReadiness $fAdCmdletUser $oChatterUser

        }

    }else{
        #No User found
        Write-Host "No user with samaccoutname found in GC"
    }

    Write-Host "`n------ Complete ------"

}

#EndRegion Functions

#Region Main body

Install-ADcmdlets
$oCsv = Import-Csv $SalesforceCsv


If (!($Users)){

    $users = get-content c:/scripts/ChatterUsers.txt
    $array = $true
}
else
{
    $Users = $User.trim()
}

$users | % {
        Check-ChatterUser $oCsv $_      
}


Do {
    $Users = ""

    If ($Users.trim() -eq ""){
        exit #Script =$true
    }else{
        $users | % {
            Check-ChatterUser $oCsv $_
        }
    }
}until(exit) #Script -eq $true)

#EndRegion Main body

答案 2 :(得分:0)

你总是可以在powershell脚本中使用.NET强制关闭。

以下是导致这种情况发生的几种方法:

[System.Environment]::Exit()

以上将导致powershell.exe应用程序以代码0退出。有一个版本的Exit()方法接受整数参数,因此您可以使用特定的退出代码退出。

[System.Diagnostics.Process]::GetCurrentProcess().Kill()

这个非常严重。它基本上称为TerminateProcess。没有退出代码,只是立即终止。