New to power shell and for the life of me can’t figure out what I’m doing wrong with my if/else statement.<\/p>","upvoteCount":2,"answerCount":10,"datePublished":"2019-02-07T18:15:27.000Z","author":{"@type":"Person","name":"brunofrisan","url":"https://community.spiceworks.com/u/brunofrisan"},"acceptedAnswer":{"@type":"Answer","text":"
Another error I get it that you are trying to set the text before you actually declare the object.<\/p>\n
try to set the if after the decalration<\/p>\n
e.g.<\/p>\n
[void] [System.reflection.Assembly]::LoadWithPartialName(\"System.drawing\")\n[void] [System.reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\")\n\n$objForm = New-object System.Windows.Forms.Form\n$objForm.Text = \"Computer Information\"\n$objForm.Size = New-Object System.Drawing.Size(300, 300)\n$objForm.StartPosition = \"CenterScreen\"\n\n$objForm.KeyPreview = $True\n$objForm.Add_KeyDown( {if ($_.KeyCode -eq \"Enter\")\n {$x = $objTextBox.Text; $objForm.Close()}})\n$objForm.Add_KeyDown( {if ($_.KeyCode -eq \"Escape\")\n {$objForm.Close()}})\n\n# Get Host Name\n$HostN = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name\n$HostN = $HostN\n\n# Host Name Label\n$objLabel = New-Object System.Windows.Forms.Label\n$objLabel.Location = New-Object System.Drawing.Size(50, 20)\n$objLabel.size = New-Object System.Drawing.Size(225, 23)\n$objLabel.Text = \"COMPUTER NAME: $HostN\"\n$OBJFORM.Controls.Add($objLabel)\n\n#Get IP\n$ip = get-WmiObject Win32_NetworkAdapterConfiguration| Where {$_.Ipaddress.length -gt 1}\n$IPADDR = $ip.ipaddress[0]\n\n# IP Label\n$objLabel = New-Object System.Windows.Forms.Label\n$objLabel.Location = New-Object System.Drawing.Size(70, 72)\n$objLabel.size = New-Object System.Drawing.Size(200, 23)\n$objLabel.Text = \"IP ADDRESS: $IPADDR\"\n$objform.Controls.Add($objLabel)\n \n#OUtput Box\n$OutputTxtbx = New-Object System.Windows.Forms.Textbox\nIf ($IPADDR -like \"192.*.*.*\") {\n $OutputTxtbx.text = \"On network\"\n}\nElse {\n $OutputTxtbx.text = \"Off network\"\n}\n$OutputTxtbx.Location = '70,100'\n$OutputTxtbx.Size = '150,30'\n$outputTxtbx.MultiLine = $True\n$outputTxtbx.BackColor = \"Black\" \n$outputTxtbx.foreColor = \"White\" \n$outputTxtbx.Borderstyle = \"none\" \n#$OutputTxtbx.scrollbars = \"vertical\"\n$objForm.Controls.Add($OutputTxtbx)\n$objForm.Topmost = $True\n$objForm.add_Shown( {$objForm.Activate()})\n[void] $objForm.ShowDialog()\n\n# OK Button\n$OKButton = New-Object System.Windows.Forms.Button\n$OKButton.Location = New-Object System.Drawing.Size(100, 200)\n$OKButton.Size = New-Object System.Drawing.Size(75, 23)\n$OKButton.Text = \"OK\"\n$OKButton.Add_Click( {$x = $objTextBox.Text; $objForm.Close()})\n$objForm.Controls.Add($OKButton)\n\n$objForm.Topmost = $True\n\n$objForm.add_Shown( {$objForm.Activate()})\n[void] $objForm.ShowDialog()\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-02-07T18:53:46.000Z","url":"https://community.spiceworks.com/t/problem-with-if-else-statement/696574/8","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"New to power shell and for the life of me can’t figure out what I’m doing wrong with my if/else statement.<\/p>","upvoteCount":2,"datePublished":"2019-02-07T18:15:27.000Z","url":"https://community.spiceworks.com/t/problem-with-if-else-statement/696574/1","author":{"@type":"Person","name":"brunofrisan","url":"https://community.spiceworks.com/u/brunofrisan"}},{"@type":"Answer","text":"
Do you have a sample What you are actually doing?<\/p>\n