从powershell登录到另一个域

时间:2016-03-20 23:09:35

标签: powershell powershell-v2.0 powershell-v3.0 powershell-remoting

我写了一个脚本来注销citrix用户并且它工作正常,如果我使用相同的域访问它,但是如果我尝试从其他本地域不同于citrix连接的计算机运行该脚本它失败了,请告诉我如何从其他本地域连接到Citrix域。

为了给出上下文,我附上下面的代码, 请帮忙。

此致 的AV

代码:

# Import the Active Directory module for the Get-ADComputer CmdLet 
Import-Module ActiveDirectory 

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

#Form to take username and password
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Citrix User Session Disconnection"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"


#Data Label
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,130)
$Label.Size = New-Object System.Drawing.Size(250,150)
$Label.ForeColor = "White"
$Label.BackColor = "Blue"
$Label.Text = "***Give your Citrix username and password to logoff the user from the server.***"
$objForm.Controls.Add($Label)


#Username Label
$userLabel = New-Object System.Windows.Forms.Label
$userLabel.Location = New-Object System.Drawing.Size(10,20)
$userLabel.Size = New-Object System.Drawing.Size(80,30)
$userLabel.Text = "User Name"
$objForm.Controls.Add($userLabel)

#Username Textbox
$userTextBox = New-Object System.Windows.Forms.TextBox
$userTextBox.Location = New-Object System.Drawing.Size(130,20)
$userTextBox.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($userTextBox)

#Password Label
$PassLabel = New-Object System.Windows.Forms.Label
$PassLabel.Location = New-Object System.Drawing.Size(10,60)
$PassLabel.Size = New-Object System.Drawing.Size(80,30)
$PassLabel.Text = "Password"
$objForm.Controls.Add($PassLabel)

#Password Textbox
$PassTextBox2 = New-Object System.Windows.Forms.MaskedTextBox
$PassTextBox2.PasswordChar = '*'
$PassTextBox2.Location = New-Object System.Drawing.Size(130,60)
$PassTextBox2.Size = New-Object System.Drawing.Size(150,20)
$objForm.Controls.Add($PassTextBox2)

#Disconnect Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(200,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.ForeColor = "Red"
$OKButton.Text = "Disconnect"
$OKButton.Add_Click({$username=$userTextBox.Text;$objForm.Close()})
$OKButton.Add_Click({$Password=$PassTextBox2.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$objForm.ShowDialog()

Read-Host "DOMAIN\USERNAME" -AsSecureString | ConvertFrom-SecureString | Out-File C:\SecureData\SecureString.txt
#SharePoint Admin Account 
$SPAdmin = "DOMAIN\ADMIN" 
$Password = Get-Content C:\SecureDate\securestring.txt | convertto-securestring 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $SPAdmin, $Password 

Get-WmiObject -Class Win32_Service -ComputerName "Server" -Filter "Name='ServiceName'" -Credential $Credential



#Authenticaton
$Domain = $env:USERDOMAIN
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain
$auth = $pc.ValidateCredentials($userName,$Password)


# Get today's date for the report 
$today = Get-Date 


#Setup email parameters 
#$subject = "ACTIVE SERVER SESSIONS REPORT - " + $today 
#$priority = "Normal" 
#$smtpServer = "mail.itechnologies.com.au" 
#$emailFrom = "rayithy@itechnologies.com.au" 
#$emailTo = "rayithy@itechnologies.com.au" 

# Create a fresh variable to collect the results. You can use this to output as desired 
$SessionList = "ACTIVE SERVER SESSIONS REPORT - " + $today + "`n`n" 

# Query Active Directory for computers running a Server operating system 
#$Servers = Get-ADComputer -Filter {OperatingSystem -like "*server*"} 
$Servers = Import-Csv C:\powershell\Test.csv

if ($auth -eq "True")
{
    # Loop through the list to query each server for login sessions 
    ForEach ($Server in $Servers) { 
        $ServerName = $Server.Name 

        # When running interactively, uncomment the Write-Host line below to show which server is being queried 
        # Write-Host "Querying $ServerName" 

        # Run the qwinsta.exe and parse the output 
        $queryResults = (qwinsta /SERVER:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)  

        # Pull the session information from each instance 
        ForEach ($queryResult in $queryResults) { 
            $RDPUser = $queryResult.USERNAME 
            $sessionType = $queryResult.SESSIONNAME 

            # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number 

            If (($RDPUser -match $username) -and ($RDPUser -ne $NULL)) {  
            # When running interactively, uncomment the Write-Host line below to show the output to screen 
            # Write-Host $ServerName logged in by $RDPUser on $sessionType 
            $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType 
            logoff $sessionType /server:$ServerName
            Write-Host $RDPUser "LoggedOff"
            }   
        } 
     } 
} 

else {
$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Italic)
#Form to display the error
$objForm2 = New-Object System.Windows.Forms.Form
$objForm2.Text = "Citrix User Session Disconnection"
$objForm2.Size = New-Object System.Drawing.Size(300,200)
$objForm2.StartPosition = "CenterScreen"
$objForm2.BackColor = "Yellow" 

#Error message
$errorLabel = New-Object System.Windows.Forms.Label
$errorLabel.Location = New-Object System.Drawing.Size(10,20)
$errorLabel.Size = New-Object System.Drawing.Size(250,150)
$errorLabel.Text = "'Username/Password is not correct' Or 'User Not Logged in the Server'"
$errorLabel.Font = $Font
$errorLabel.forecolor = "Red"
$objForm2.Controls.Add($errorLabel)
$objForm2.ShowDialog()

}

1 个答案:

答案 0 :(得分:0)

在脚本的“身份验证”部分中:

#Authenticaton
$Domain = $env:USERDOMAIN 
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain 
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$Domain 
$auth = $pc.ValidateCredentials($userName,$Password) 

您传入$ Domain作为当前用户的域名。如果您修改此选项以指定Citrix身份验证的域,则应该能够连接