Pablo's Powershell Pow-Wow

Discussion in 'Business & Enterprise Computing' started by PabloEscobar, Sep 18, 2014.

  1. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,409
    Location:
    Melbourne
    I would like to know if you find a way

    Some can be added by updating, but some just dont work

    You might be able to use the WMI commands that relate to what you are trying to find. (This is how I have done it in the past)
     
  2. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    17,590
    Location:
    Canberra
    so... short of entering the Session, directly via enter-pssession, I can't get access to the new cmdlets.

    invoke-command fails :/ both with -computer and/or -session.
     
  3. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,409
    Location:
    Melbourne
    The issue is PSSession is that you a running the command on the remote PC
     
  4. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    13,269
    Why is that an issue :)

    Constrained Management Endpoints FTW.
     
  5. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    6,178
    Location:
    Brisbane, QLD
    Code:
    # Records computer and user details and inserts into database
    Function CreateLogonRecord{
    
        $conn = New-Object System.Data.SqlClient.SqlConnection
    
        # Edit this for your school and database
        $conn.ConnectionString = "Data Source=SERVER\DATABASE;Initial Catalog=TABLE;Integrated Security=SSPI;"
        $conn.open()
    
        $cmd = New-Object System.Data.SqlClient.SqlCommand
        $cmd.connection = $conn
    
            # Computer and user details
            $wsname = $env:COMPUTERNAME
            $os = Get-WMIObject Win32_OperatingSystem -ComputerName $wsname
            $system = Get-WmiObject win32_ComputerSystem -ComputerName $wsname 
            $network = Get-WmiObject win32_networkadapterconfiguration -ComputerName $wsname | where {$_.Ipenabled -Match "True"}
            $chassis = Get-WmiObject Win32_BIOS -ComputerName $wsname
            $build = Get-ItemProperty HKLM:\Software\SOE | Select -ExpandProperty 'Build version'
    
        # SQL Command to run. Values after -f need to match the array
        $cmd.commandtext = "INSERT INTO [PC_Info].[dbo].[Logons] `
            (UserName,PCName,PCDescription,MAC,Model,OS,MOE_Build,SMBIOS_Serial) `
            VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')" `
            -f $env:USERNAME,$wsname,$os.Description,$network.MACAddress,$system.Model,$os.Caption,$build,$chassis.SerialNumber
        
        # Execute query and close connection
        $cmd.executenonquery()
        $conn.close()
    
    }
    How close did I get?
     
  6. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,136
    Location:
    Brisbane
     
  7. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    13,269
    It probably works.
    But WMI makes baby jesus cry.
     
  8. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    6,178
    Location:
    Brisbane, QLD
    Go for it! I think a lot of my habits carry over from using other programming languages.
     
  9. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,136
    Location:
    Brisbane
    What operating system does this have to run on, and what will be the minimum version of PowerShell?
     
  10. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    6,178
    Location:
    Brisbane, QLD
    We have a few versions of Windows 7, but mostly 8 and 10. I can't actually check what the minimum version as I'm not back at work for a fortnight. I think everyone has PS1 or PS3.
     
  11. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,136
    Location:
    Brisbane
    Well the difference between PS1 and PS3 is fundamental. I won't bother putting the work in until you can confirm. I really hope you'd be at PSv2 as a minimum.
     
  12. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    6,178
    Location:
    Brisbane, QLD
    Code:
    PS C:\Users\user> $host.version
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    3      0      -1     -1
    Better late than never right? This is on the DC where the script would be run, everything else (that mattes) is at or above this level.
     
    Last edited: Jul 18, 2017
  13. Dre_

    Dre_ Member

    Joined:
    May 25, 2014
    Messages:
    824
    How has this thread not had any posts for so damn long?

    Anyway, is there a way to see how long something takes to run?

    I'd like to see how quickly this runs, and compare it to a few different scripts.
    Code:
    Get-AzureStorageTableRowByPartitionKey -table $storageTable -partitionKey $partitionKey1 | ft
    Second thought, I Googled it....

    https://docs.microsoft.com/en-us/pr...windows-powershell-1.0/ee176899(v=technet.10)

    So would it be this?

    Code:
    Measure-Command {Get-AzureStorageTableRowByPartitionKey -table $storageTable -partitionKey $partitionKey1 | ft}
     
    Last edited: Jan 23, 2018
  14. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,136
    Location:
    Brisbane
    Yes.

    You should be aware that formatting the result will effect the time taken to run the command, if you're trying to compare it something (e.g. In your above example, Format-Table).
     
  15. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    13,269
    Putting anything on the screen incurs a big time penalty as well, if you feel the need for speed... then you better not also feel the need to see.
     
  16. Ogre

    Ogre Member

    Joined:
    Aug 13, 2003
    Messages:
    2,437
    Location:
    Sydney, Australia
    Hi Guys,

    I was wondering if anyone would be able to give me some tips to clean up/speed up this:

    Code:
    Import-Module DhcpServer
    
    $clients = Import-Csv "C:\pathtocsv.csv"
    
    $server = "dhcpserver.domain.com"
    
    foreach ($client in $clients) {
    
        $reservation = Get-DhcpServerv4Scope -ComputerName $server |
                       Get-DhcpServerv4Reservation -ComputerName $server |
                       Where-Object {$_.Ipaddress -eq $client.Ipaddress}
    
        if ($reservation) {
            $reservation | Remove-DhcpServerv4Reservation -ComputerName $server
            Add-DhcpServerv4Reservation -ComputerName $server -ScopeId $client.scopeid -IPAddress $client.Ipaddress -Name $client.Name -ClientId $client.ClientID -Description $client.Description
        } else {
            Add-DhcpServerv4Reservation -ComputerName $server -ScopeId $client.scopeid -IPAddress $client.Ipaddress -Name $client.Name -ClientId $client.ClientID -Description $client.Description
        }
    
    }
    Basically we are getting a new building the AV/IT Vendors are giving us some fairly incomplete data which means multiple imports into DHCP.

    This script grabs the ScopeId, IPAddress, Name, ClientID, and Description to create DHCP reservations. It also check to see if one already exists for the IPAddress, then deletes it and adds it back again with the updated data.

    I believe the slowest part is when it grabs the scopes and reservations from the sever each time.

    Any help is great appreciated.
     
  17. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    17,590
    Location:
    Canberra
    seems obvious that you'd simply grab the reservation info into an array beforehand, then process it in your loop afterwards...
     
  18. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    13,269
    You'd be correct.
    You're enumerating the whole DHCP Scope each time... unless things are changing rapidly... and likely to change in between processing Client1 and processing Client2, you can shift that whole bit outside your loop, and just return an Array, that you can use inside your loop. (Looping through an already build array is going to be an order of magnitude faster than asking the DHCP server for all the info each time).

    Give it a shot, and post up what you end up with, if you still can't get it to work, I'll give it a shot tomorrow.

    How much data are we talking about? As is often the case, the time taken to speed up the code, will be longer than just letting it run, especially if its not something that's running all the time.
     
  19. Ogre

    Ogre Member

    Joined:
    Aug 13, 2003
    Messages:
    2,437
    Location:
    Sydney, Australia
    Thanks guys

    We are running it a few times a day while the guys on site sort out all the issues. There are roughly 3000 devices in the source spreadsheet.
     
  20. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,136
    Location:
    Brisbane
    What to provide a couple of lines of your example CSV?

    Straight off the top of my head, do any of those DHCP commands do a progress bar? If so, simply setting $ProgressPreference to 'SilentlyContinue' will dramatically improve your processing speed.

    The more important part is the iteration of the scopes within your loop, that both NSanity and Pablo have mentioned however...
     

Share This Page

Advertisement: