Welcome to WebmasterWorld Guest from 3.92.92.168

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need help to convert PS to PHP

     
6:12 am on Apr 11, 2016 (gmt 0)

New User

joined:Apr 11, 2016
posts:1
votes: 0


I've got the following PowerShell code that logs out citrix users from servers, but I have to project this as a webpage, I am hoping some help to covnert this code to PHP or to call this ps1 from PHP.

Many thanks in advance.

Here is the code:
# 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

#Intuit Logo color
$intuitblue = [System.Drawing.Color]::FromArgb(48,145,173)

#Form to take username and password
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Citrix User Session Self Logout"
$objForm.Size = New-Object System.Drawing.Size(600,600)
$Image = [system.drawing.image]::FromFile("C:\CitrixSessionLogout\Intuit_logo_NoBackground.png")
$objForm.BackgroundImage = $Image
$objForm.BackgroundImageLayout = "Center" # None, Tile, Center, Stretch, Zoom
$objForm.StartPosition = "CenterScreen"
$objForm.BackColor = "Grey"


#Form Title
$FormTitle = New-Object System.Windows.Forms.Label
$FormTitle.Location = New-Object System.Drawing.Size(80,20)
$FormTitle.AutoSize = $True
$FormTitle.Text = "Citrix User Session Self Logout"
$FormTitleFont = New-Object System.Drawing.Font("Calibri",24,[System.Drawing.FontStyle]::Bold)
$FormTitle.Font = $FormTitleFont
$FormTitle.ForeColor = $intuitblue
$objForm.Controls.Add($FormTitle)

#Data Label
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(100,540)
$Label.AutoSize = $True
$Label.ForeColor = "White"
$Label.BackColor = $intuitblue
$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,120)
$userLabel.AutoSize = $True
$userLabel.Text = "User Name"
$userLabelFont = New-Object System.Drawing.Font("Calibri",14)
$userLabel.Font = $userLabelFont
$userLabel.ForeColor = $intuitblue
$objForm.Controls.Add($userLabel)

#Username Textbox
$userTextBox = New-Object System.Windows.Forms.TextBox
$userTextBox.Location = New-Object System.Drawing.Size(130,120)
$userTextBox.Size = New-Object System.Drawing.Size(150,20)
$userTextBoxFont = New-Object System.Drawing.Font("Calibri",12)
$userTextBox.Font = $userTextBoxFont
$userTextBox.ForeColor = $intuitblue
$objForm.Controls.Add($userTextBox)

#Password Label
$PassLabel = New-Object System.Windows.Forms.Label
$PassLabel.Location = New-Object System.Drawing.Size(10,160)
$PassLabel.AutoSize = $True
$PassLabel.Text = "Password"
$PassLabelFont = New-Object System.Drawing.Font("Calibri",14)
$PassLabel.Font = $PassLabelFont
$PassLabel.ForeColor = $intuitblue
$objForm.Controls.Add($PassLabel)

#Password Textbox
$PassTextBox2 = New-Object System.Windows.Forms.MaskedTextBox
$PassTextBox2.PasswordChar = '*'
$PassTextBox2.Location = New-Object System.Drawing.Size(130,160)
$PassTextBox2.Size = New-Object System.Drawing.Size(150,20)
$PassTextBox2Font = New-Object System.Drawing.Font("Calibri",12)
$PassTextBox2.Font = $PassTextBox2Font
$PassTextBox2.ForeColor = $intuitblue
$objForm.Controls.Add($PassTextBox2)

#Disconnect Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(180,200)
$OKButton.AutoSize = $True
$OKButton.ForeColor = "Red"
$OKButton.BackColor = "Yellow"
$OKButtonFont = New-Object System.Drawing.Font("Calibri",14)
$OKButton.Font = $OKButtonFont
$OKButton.Text = "Disconnect"
$OKButton.Add_Click({$username=$userTextBox.Text;$objForm.Close()})
$OKButton.Add_Click({$Password=$PassTextBox2.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

#Form to display the error
$objForm2 = New-Object System.Windows.Forms.Form
$objForm2.Text = "Citrix User Session Self Logout"
$objForm2.Size = New-Object System.Drawing.Size(400,400)
$objForm2.BackgroundImageLayout = "Center" # None, Tile, Center, Stretch, Zoom
$objForm2.StartPosition = "CenterScreen"
$objForm2.BackColor = "Grey"

#Intuit Logo
$Image = [system.drawing.image]::FromFile("C:\CitrixSessionLogout\intuitlogo.PNG")
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $logo.Size.Width
$pictureBox.Height = $logo.Size.Height
$pictureBox.Image = $logo
$pictureBox.Location = New-Object System.Drawing.Size(140,10)
$objForm2.Controls.Add($pictureBox)

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


$objForm.ShowDialog()

#Authenticaton
#Read Domains from the List
$Domains = Import-Csv C:\powershell\Domains.csv
$auth = $false

#search the users from the domains list and authenticate
ForEach ($domain in $Domains) {
#Write-Host $auth
if ($auth -eq $false)
{
$DomainName = $domain.Domain
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$DomainName
$auth = $pc.ValidateCredentials($username,$Password)
#write-host $DomainName

}
}


# 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

#Once Authentication complete go to logging off the user from the server
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

#Form to display the Update
$objForm3 = New-Object System.Windows.Forms.Form
$objForm3.Text = "Citrix User Session Self Logout"
$objForm3.Size = New-Object System.Drawing.Size(400,400)
$objForm3.BackgroundImageLayout = "Center" # None, Tile, Center, Stretch, Zoom
$objForm3.StartPosition = "CenterScreen"
$objForm3.BackColor = "Grey"

#Add Intuit Logo
$objForm3.Controls.Add($pictureBox)

#Success message
$errorLabel2 = New-Object System.Windows.Forms.Label
$errorLabel2.Location = New-Object System.Drawing.Size(50,150)
$errorLabel2.Size = New-Object System.Drawing.Size(350,250)
$errorLabel2.Text = $RDPUser + " has been Logged Off"
$errorLabelFont2 = New-Object System.Drawing.Font("Calibri",16)
$errorLabel2.Font = $errorLabelFont2
$errorLabel2.forecolor = $intuitblue
$objForm3.Controls.Add($errorLabel2)

$objForm3.ShowDialog()
}
}
}
}

#If authentication fails show the authentication failure
else {
$objForm2.ShowDialog()

}


2:41 pm on Apr 21, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Welcome to WebmasterWorld, Test_me!
Perhaps the Program Execution Functions will work for you?
[php.net...]