none
Help with a couple basic functions - Getting PC name by MAC and Removing PC by MAC (SCCM) RRS feed

  • Question

  • Function Remove-PCByMAC {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [string]$MAC
        )
    
    $SiteServer = 'SCCM'
    $SiteCode = '000'
    
    $comp = $null
    $query = "SELECT * FROM SMS_R_SYSTEM WHERE MACAddresses like '$MAC'"
    $comp = Get-WmiObject -ComputerName $siteserver -Namespace root\sms\site_$sitecode -Query $query
    $comp.delete()
    }
    
    Function Get-PCByMAC {
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)]
            [string]$MAC
        )
    
    $SiteServer = 'SCCM'
    $SiteCode = '000'
    
    $comp = $null
    $query = "SELECT * FROM SMS_R_SYSTEM WHERE MACAddresses like '$MAC'"
    $comp = Get-WmiObject -ComputerName $SiteServer -Namespace root\sms\site_$SiteCode -Query $query | Select-Object Name
    $comp
    }

    I stole/modified these from Jason Sandys & a post at forthcomputing, they work great in ISE or powershell with dot sourcing. However, when attempting to implement them in a GUI app I'm getting some wonky errors. I'm pretty new to building GUI's with XAML/WPF/POSH. Can anyone point me to some online resources where I can further my knowledge and fix this myself, or just drop some knowledge bombs on me?

    Example error

    Get-PCByMAC : Cannot bind argument to parameter 'MAC' because it is an empty string.
    At C:\TEMP\Launch-LightweightConsole 2019.11.22.ps1:2474 char:22
    +     Get-PCByMAC -MAC $MAC_TargetTextBox.Text
    +                      ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Get-PCByMAC], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Get-PCByMAC
     

    Some XAML

    			<TabItem Header="MAC">
    				<Grid>
    					<GroupBox x:Name="MAC_RemovalGroupBox" Header="MAC Address" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="115" Width="498">
    					    <TextBlock x:Name="MAC_WarningTextBlock" HorizontalAlignment="Left" Margin="5,60,0,0" TextWrapping="Wrap" Text="MAC Address should be entered in format 00:00:00:00:00:00" VerticalAlignment="Top" Foreground="#FFF12525"/>
                        </GroupBox>
                        <TextBox x:Name="MAC_TargetTextBox" HorizontalAlignment="Left" Height="23" Margin="20,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="190" ToolTip="Format: 00:00:00:00:00:00" TabIndex="2"/>
    			        <Button x:Name="MAC_InspectButton" Content="Get Computer Name" HorizontalAlignment="Left" Margin="229,37,0,0" VerticalAlignment="Top" Width="129" RenderTransformOrigin="0.173,0.15" TabIndex="7" Grid.ColumnSpan="2" />
                        <Button x:Name="MAC_PerformButton" Content="Remove from SCCM" HorizontalAlignment="Left" Margin="365,37,0,0" VerticalAlignment="Top" Width="129" RenderTransformOrigin="0.173,0.15" TabIndex="7" />
    				</Grid>
    			</TabItem>

    More XAML/WPF stuff

    #MAC TAB
    
    $MAC_RemovalGroupBox = $SyncHash.MAC_RemovalGroupBox = $SyncHash.Window.FindName("MAC_RemovalGroupBox")
    $MAC_TargetTextBox = $SyncHash.MAC_TargetTextBox = $SyncHash.Window.FindName("MAC_TargetTextBox")
    $MAC_InspectButton = $SyncHash.MAC_InspectButton = $SyncHash.Window.FindName("MAC_InspectButton")
    $MAC_PerformButton = $SyncHash.MAC_PerformButton = $SyncHash.Window.FindName("MAC_PerformButton")

    And of course the actual code, which I believe I understand why the Set-StatusBarText portion is not (working) and will not work, but I'm failing to understand why the basic functions are returning the null/empty string error(s).

    #region MAC TAB
    ###########
    
    $MAC_TargetTextBox = $MAC_TargetTextBox.Add_TextChanged
    
    $MAC_InspectButton = $MAC_InspectButton.Add_Click({
        Get-PCByMAC -MAC $MAC_TargetTextBox.Text
            Set-StatusBarText "$comp identified in SCCM."
    })
    
    $MAC_PerformButton = $MAC_PerformButton.Add_Click({
        Remove-PCByMAC -MAC $MAC_TargetTextBox.Text
            Set-StatusBarText "$comp removed from SCCM."
    })
    
    #endregion





    • Edited by JasonUlbright Friday, December 27, 2019 9:15 PM words
    Friday, December 27, 2019 9:08 PM

Answers

  • You need to define the variable or get the object.

    $tb = $this.FindName("MAC_TargetTextBox")

    Unfortunately what you have posted does not tell us what code is being executed and where.

    The following line does nothing:

    $MAC_TargetTextBox =$MAC_TargetTextBox.Add_TextChanged

    You are just assigning null to the variable so the variable is now useless.



    \_(ツ)_/

    • Marked as answer by JasonUlbright Monday, December 30, 2019 7:19 PM
    Friday, December 27, 2019 9:51 PM

All replies

  • Clearly the object being referenced is null.

    \_(ツ)_/

    Friday, December 27, 2019 9:18 PM
  • Should I be using something other than .Add_TextChanged or .Text? The error indicates null, so the script isn't reading from the textbox?
    Friday, December 27, 2019 9:30 PM
  • You need to define the variable or get the object.

    $tb = $this.FindName("MAC_TargetTextBox")

    Unfortunately what you have posted does not tell us what code is being executed and where.

    The following line does nothing:

    $MAC_TargetTextBox =$MAC_TargetTextBox.Add_TextChanged

    You are just assigning null to the variable so the variable is now useless.



    \_(ツ)_/

    • Marked as answer by JasonUlbright Monday, December 30, 2019 7:19 PM
    Friday, December 27, 2019 9:51 PM
  • Suggestion. Drop the runspace and build your form as a simple script. When the script works then wrap it in a runspace.


    \_(ツ)_/

    Friday, December 27, 2019 9:52 PM
  • $MAC_InspectButton = $MAC_InspectButton.Add_Click({
        $comp = Get-PCByMAC -MAC $MAC_TargetTextBox.Text
        $comp
            Set-StatusBarText "$comp identified in SCCM."
    })
    
    $MAC_PerformButton = $MAC_PerformButton.Add_Click({
        $comp = Get-PCByMAC -MAC $MAC_TargetTextBox.Text
        $comp
        Remove-PCByMAC -MAC $MAC_TargetTextBox.Text
            Set-StatusBarText "$comp removed from SCCM."
    })
    
    That worked. Thanks!
    Monday, December 30, 2019 7:18 PM
  • The script was never an issue. It was my lack of knowledge.

    Your above reply, coupled with this amazing tutorial from https://adamtheautomator.com/build-powershell-gui/ helped me figure it out.

    Now to move on to learning more about this amazing tool.

    Thank you again!

    Monday, December 30, 2019 7:22 PM