New to Powershell Scripts and need help.

I am trying to create a script that I can give to people to collect information off a local PC or remote.</p>
I want the script to ask for an IP or if left blank get the local PC’s info. Everything seems right, but it doesn’t work :(</p>

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter IP address. leave Blank if local PC'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $Computer = $textBox.Text

} else{ ([string]::IsNullOrEmpty($Computer)) = (env:COMPUTERNAME)

}

$Connection = Test-Connection $Computer -Count 1 

if ($Connection -eq "True"){

$ComputerHW = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer | select Manufacturer,Model | FT -AutoSize
    

   Write-Host "Computer Model: $ComputerHW"
}

else {
   Write-Host -ForegroundColor Red @"

Computer is not reachable or does not exists.

"@
}

I have a bunch more information I want to collect, but can’t even get a single return. Please HELP! Thanks in advance.

 

 

 

Seems like a lot of work building a form just to ask for computername. Why not just Read-Host? Anyway, when a troubleshot your script it was hanging on:

$form.Add_Shown({$textBox.Select()})

So I modified this a little and got it running. You have a couple logic issues with assigning the computername in the event of null entry. Also, just because you can ping a computer Test-Connection does not mean you can run Get-WMIObject -computername … Get-WMIObject (which is deprecated) uses RPC and ping is ICMP so still may not work. Get-CIMInstance is the preferred method now. It uses WinRM. Anyway, I left that as is. Here is working code using your script below.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter IP address. leave Blank if local PC'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

#$form.Add_Shown({$textBox.Select()})
$form.Controls.Add($textBox)
$textBox.Select()
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    If ($textBox.Text -eq $null) {
        $Computer = $env:COMPUTERNAME
    } # if no value entered for ip
    else {
        $Computer = $textBox.Text
    } # else value entered for ip
    #pinging computer, btw, being able to ping does not mean able to RPC or WinRM..
    $Connection = Test-Connection $Computer -Count 1 

    if ($Connection -eq "True"){
        $ComputerHW = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer | select Manufacturer,Model | FT -AutoSize
        Write-Host "Computer Model: $ComputerHW"
    } #if able to ping
    else {
       Write-Host -ForegroundColor Red "Computer is not reachable or does not exists."
    } #else not able to ping
} #if click OK on form
else {
    Write-Host "Canceled"
} #clicked cancel/x on form

 

Awesome thanks Mike, yeah after I posted I ran into the WinRM issue as well. I need to pull a lot more information, but that’s the easy part. I currently have a bat file that does everything using PowerShell, but it has to be copied and ran per box. A co-worker sent me down this rabbit hole telling me a PowerShell script will be much cleaner and be able to run remotely.

<style></style>

Your co-worker is right. A PowerShell script would be much cleaner than a bat file (running PS commands…?) copied to each machine. If there is lots of data you need to collect, that is fine. Put it all in one script then call the script for every host you need data from. For example imagine the following script saved as datacollection.ps1:

$computerinfo = @{}
# Hardware Info
$ComputerInfo.Hardware = Get-CimInstance -ClassName win32_ComputerSystem |
    Select-Object -Property Manufacturer, Model

# Scheduled Tasks
$computerinfo.scheduledtasks = Get-ScheduledTask

# Running Processes
$computerinfo.processes = Get-Process

[pscustomobject]$computerinfo

Then to run this script on remote machine(s) use invoke-command like this:

$targets = "sv-1", "sv-2"
$results = Invoke-Command -ComputerName $targets -FilePath ".\datacollection.ps1"

Then the collected data would be stored in $results and you can filter, sort, export to your hearts desire :slight_smile: