0% found this document useful (0 votes)
9 views3 pages

Poweshell GU

This PowerShell script creates a GUI tool for copying files to remote servers, requiring administrative privileges to run. It allows users to select a source file, specify a target directory, and input a list of servers, with functionality to check server connections and copy the specified file. The GUI is built using XAML and includes output logging for connection and copy status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Poweshell GU

This PowerShell script creates a GUI tool for copying files to remote servers, requiring administrative privileges to run. It allows users to select a source file, specify a target directory, and input a list of servers, with functionality to check server connections and copy the specified file. The GUI is built using XAML and includes output logging for connection and copy status.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# Ensure the script runs with admin privileges

if (-not ([Security.Principal.WindowsPrincipal]
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.Wi
ndowsBuiltInRole] "Administrator")) {
Write-Host "Run this script as Administrator." -ForegroundColor Red
exit
}

# Import necessary assemblies for WPF


Add-Type -AssemblyName PresentationFramework

# Define the XAML for the GUI


$XAML = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="File Copy Tool" Height="450" Width="700">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<TextBlock Text="Source File:" Grid.Row="0" Grid.Column="0"


VerticalAlignment="Center" Margin="0,0,10,0"/>
<TextBox Name="SourceFilePath" Grid.Row="0" Grid.Column="0"
Margin="100,0,10,0"/>
<Button Name="BrowseSourceFile" Content="Browse..." Grid.Row="0"
Grid.Column="1" Width="75" HorizontalAlignment="Right"/>

<TextBlock Text="Target Directory on Servers:" Grid.Row="1" Grid.Column="0"


VerticalAlignment="Center" Margin="0,10,10,0"/>
<TextBox Name="TargetDirectory" Grid.Row="1" Grid.Column="0"
Margin="100,10,10,0"/>

<TextBlock Text="Server List:" Grid.Row="2" Grid.Column="0"


VerticalAlignment="Top" Margin="0,10,10,0"/>
<TextBox Name="ServerList" AcceptsReturn="True"
VerticalScrollBarVisibility="Auto" Grid.Row="2" Grid.Column="0"
Margin="100,10,10,0" Height="150"/>

<Button Name="CheckConnection" Content="Check Connection" Grid.Row="3"


Grid.Column="0" Width="150" Margin="0,10,0,0" HorizontalAlignment="Left"/>
<Button Name="CopyFiles" Content="Copy Files" Grid.Row="3" Grid.Column="1"
Width="150" Margin="10,10,0,0" HorizontalAlignment="Right"/>

<TextBox Name="Output" IsReadOnly="True" AcceptsReturn="True"


VerticalScrollBarVisibility="Auto" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2"
Height="200" Margin="0,10,0,0"/>
</Grid>
</Window>
"@

# Load the XAML


$xmlDocument = New-Object System.Xml.XmlDocument
$xmlDocument.LoadXml($XAML)
$reader = New-Object System.Xml.XmlNodeReader $xmlDocument
$window = [Windows.Markup.XamlReader]::Load($reader)

# Find GUI elements


$SourceFilePath = $window.FindName("SourceFilePath")
$BrowseSourceFile = $window.FindName("BrowseSourceFile")
$TargetDirectory = $window.FindName("TargetDirectory")
$ServerList = $window.FindName("ServerList")
$CheckConnection = $window.FindName("CheckConnection")
$CopyFiles = $window.FindName("CopyFiles")
$Output = $window.FindName("Output")

# Function to write to the output box


function Write-OutputBox {
param ($message)
$Output.AppendText("$message`n")
$Output.ScrollToEnd()
}

# Browse for the source file


$BrowseSourceFile.Add_Click({
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$dialog = New-Object System.Windows.Forms.OpenFileDialog
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$SourceFilePath.Text = $dialog.FileName
}
})

# Check connection to servers


$CheckConnection.Add_Click({
$Output.Clear()
$servers = $ServerList.Text -split "`n" | Where-Object { $_.Trim() -ne "" }
foreach ($server in $servers) {
if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
Write-OutputBox "Connection to $server: Success"
} else {
Write-OutputBox "Connection to $server: Failed"
}
}
})

# Copy files to remote servers


$CopyFiles.Add_Click({
$Output.Clear()
$sourceFile = $SourceFilePath.Text
$targetDir = $TargetDirectory.Text

if (-not (Test-Path $sourceFile)) {


Write-OutputBox "Source file not found. Please select a valid file."
return
}

if ([string]::IsNullOrEmpty($targetDir)) {
Write-OutputBox "Target directory is empty. Please provide a target
directory."
return
}

$servers = $ServerList.Text -split "`n" | Where-Object { $_.Trim() -ne "" }


foreach ($server in $servers) {
if (Test-Connection -ComputerName $server -Count 1 -Quiet) {
try {
Write-OutputBox "Copying file to $server..."
$remotePath = "\\$server\$($targetDir -replace '^C:\\', 'C$')\$
(Split-Path $sourceFile -Leaf)"
Copy-Item -Path $sourceFile -Destination $remotePath -Force -
ErrorAction Stop
Write-OutputBox "File copied to $server: $remotePath"
} catch {
Write-OutputBox "Failed to copy file to $server: $_"
}
} else {
Write-OutputBox "Cannot connect to $server. Skipping..."
}
}
})

# Show the window


$window.ShowDialog()

You might also like