1. If you're receiving a message that you are banned from the Current Events or Politics forums, it's not you specifically: those forums have been hidden for all users. For more info, see here.
    Dismiss Notice

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,507
    Location:
    Melbourne
    There are restricted fields (from my understanding) so they cant just put anything in there

    We also have Dynamic distribution groups, but all of this was put in before i started so I can only hope its all correct :)
     
  2. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Usually the scripts that I'm giving the end user to use do not require any real elevation of their existing user accounts. It's more automation than anything else.


    In this case, you could use one of the extended attributes in AD to say what a "real" manager is and set the Dynamic Distribution group on that property instead.

    As for the HR integration with AD, I've not worked in a place that hasn't had a separate HR database that is syncronised with AD, either via some script, (horrendous or otherwise), e.g. in my current environment a Perl Script pulls all the properties from the database and updates AD with results (it's ugly as hell and inefficient to boot). In other places it's been SQL Server Integration Services running and updating AD, or other systems that can federate with AD and update the properties directly.

    I think if I were to do what you were doing with the tools you've described, I would create a front end in Orchestrator and use the AD connectors to do the job after validating a list of Job Titles from a table in database (or just a simple validation script).
     
  3. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    Its the front end in Orchestrator that is the ball sucking part of it. The idea is good, but the execution is poor. as an automation framework, Orchestrator is terrible to work with, Jenkins seems much nicer, but I'm still not convinced its the right tool for the job.
     
  4. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Yeah, my experience is lacking in this regard.

    The only place I've seen implement anything like what you're talking about was getting a webdev to knock up some simple forms in ASPX and either converted the PowerShell to native .net or did some trickery to call the PowerShell on the server-side with the inputs from the website. I didn't like the amount of maintenance and overhead you had to have however...
     
  5. looktall

    looktall Working Class Doughnut

    Joined:
    Sep 17, 2001
    Messages:
    26,665
    i'm trying to set a variable in a script.

    it works perfectly fine right up until the point where the folder name in the path contains a hyphen.

    $variable = "C:\Applications\Hyphen-Name"

    i keep getting an error "unexpected token '-Name' in expression or statement."

    i've tried double and single quotes, brackets all sorts of things, but i can't get it to accept the path with a hyphen in it.

    changing the folder name to remove the hyphen is not an option.

    any ideas?
     
  6. wullieb1

    wullieb1 Member

    Joined:
    Jul 9, 2013
    Messages:
    471
    Have you tried the escape character?

    $variable = "c:\Applications\Hyphen'-name"
     
  7. looktall

    looktall Working Class Doughnut

    Joined:
    Sep 17, 2001
    Messages:
    26,665
    I did but may not have used it correctly.
    I'll try it again tomorrow.
     
  8. wullieb1

    wullieb1 Member

    Joined:
    Jul 9, 2013
    Messages:
    471
    I've just ran your command on my Win 8.1 system and it works fine with no escape key.

    What version of PowerShell are you running?

    Windows PowerShell
    Copyright (C) 2014 Microsoft Corporation. All rights reserved.

    PS C:\Users\wullieb1> $variable = "c:\hypen-test"
    PS C:\Users\wullieb1> write-host $variable
    c:\hypen-test
    PS C:\Users\wullieb1> $variable = "c:\hypen-Name"
    PS C:\Users\wullieb1> write-host $variable
    c:\hypen-Name
     
  9. looktall

    looktall Working Class Doughnut

    Joined:
    Sep 17, 2001
    Messages:
    26,665
    I think I was running it on 2012 R2 or 8.1.
    I can't recall. I had a lot of connections open at the time.

    I don't expect there's much different between them.

    EDIT: i've just tried it now myself at home and it also worked.
    this got me thinking about what it was i was actually trying to do and i figured it out.

    i was also trying to put a hyphen in the variable name.
    i removed the hyphen from the variable (which is no big deal) and it worked straight away.

    i guess that's what happens when you're rushing to complete something at the end of a busy day.

    thanks anyway
     
    Last edited: Feb 13, 2017
  10. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    Here-Strings are fun (https://technet.microsoft.com/en-us/library/ee692792.aspx)

    Code:
    PS C:\Users\Pablo> $X = @"
    >> I'm a stupidly long string, with-lots-of-fucked-punctuation! and even an invoke-command
    >> I've even got a carriage return in me
    >> what the fuck am I doing
    >> I've even got "double" and 'single' and `backticks` and ~tildes~ and a couple of #octothorpes#
    >> @
    >> 8
    >> &
    >> 6
    >> %
    >> it really doesn't matter what goes in here
    >> "@
    
    Output

    Code:
    PS C:\Users\Pablo> write-host $x
    I'm a stupidly long string, with-lots-of-fucked-punctuation! and even an invoke-command
    I've even got a carriage return in me
    what the fuck am I doing
    I've even got "double" and 'single' andackticks and ~tildes~ and a couple of #octothorpes#
    @
    8
    &
    6
    %
    it really doesn't matter what goes in here
    
    @" on one line
    and @" on its own line
    everything in between is just a string.

    Useful at the end of a long day, when you can't work out which quotes (as long as they aren't backticks it would seem) to use where, and why.
     
  11. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    I agree, they're great. The only thing that bugs me is how much they mess up your formatting, e.g.

    Code:
    function MyFunction ($param1, $param2)
    {
        if ($x -gt $y)
        {
            $here =@"
            sadfasdfsdf
            asdf
            sadfasdf
            asdfas
            dfsdf
            234
    
            2349087
            *^%(*!@&#_!~_
    "@ # <This bit looks ugly
        }
    }
    
     
  12. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    7,409
    Location:
    Mount Cotton, QLD
    Can this one liner be improved on?

    Code:
    get-aduser -filter * -searchbase $students -properties SamAccountName, HomeDirectory | select-object SamAccountName, HomeDirectory | Export-csv -path D:\Students2.csv
    Note: $students expands to

    Code:
    OU=2176_Students,OU=2176_Users,OU=2176_Marsden SHS,OU=5625_LA,DC=SOC,DC=eq,DC=edu,DC=au
    Further to that, how could I limit results to HomeDirectory properties that start with \\EQSOC2176001 ?
     
  13. wullieb1

    wullieb1 Member

    Joined:
    Jul 9, 2013
    Messages:
    471
    Use a where-object command.

    Something like

    Code:
    get-mailbox -resultsize unlimited | Where-Object {$_.Office -eq "Singapore"}
    (Yes i know not relevant to your command but it might help)

    So maybe you should use sometyhing like

    Code:
    get-aduser -filter * -searchbase $students -properties SamAccountName, HomeDirectory | Where-Object {$_.HomeDirectory -eq "\\EQSOC2176001"} | select-object SamAccountName, HomeDirectory | Export-csv -path D:\Students2.csv
     
  14. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    7,409
    Location:
    Mount Cotton, QLD
    Ahh I did try that second line but I didn't have another "| select-object" after the where part.
     
  15. MikHail

    MikHail Member

    Joined:
    Feb 8, 2003
    Messages:
    434
    Location:
    Sydney
    ^^^

    FYI, you can use select instead of select-object

    Code:
    dir | select name


    also you can use question mark instead of where-object

    Code:
    dir | ? {$_.name -like "*a*"}



    If you're using powershell 3 or Windows 8 or windows 2012

    Code:
    dir | ? name -like "*a*"
     
  16. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
  17. m0n4g3

    m0n4g3 Member

    Joined:
    Aug 5, 2009
    Messages:
    3,741
    Location:
    Perth, WA
    amen!

    If you are one linering powershell commands sure... use aliases... that's what they are there for, but for scripts make it as readable for everyone (beginner and veteran alike ;) )
     
  18. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,507
    Location:
    Melbourne
    Hey Powershell People,

    I have a query for you

    I currently have a array with data displaying as :

    Code:
    Date	ServerName			Average
    Jan-17	Server0002.domain.local		76.87
    Jan-17	Server0003.domain.local		76.25
    Jan-17	Server0004.domain.local		72.79
    Feb-17	Server0002.domain.local		78.018
    Feb-17	Server0003.domain.local		67.872
    Feb-17	Server0004.domain.local		74.638
    Dec-16	Server0002.domain.local		69.02
    Dec-16	Server0003.domain.local		73.61
    Dec-16	Server0004.domain.local		83.46
    I need to convert to to be in this format

    Code:
    ServerName			Dec-16		Jan-17		Feb-17
    Server0002.domain.local		69.02		76.87		78.018
    Server0003.domain.local		73.61		76.25		67.872
    Server0004.domain.local		83.46		72.79		74.638
    Is this possible, any tips ?
     
  19. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Yes, it's possible and if there isn't a better way than this then I'll be incredibly surprised.

    Code:
    $original = Import-Csv -Path C:\temp\import.csv
    
    $dates = $original | Select-Object -ExpandProperty Date -Unique
    $servers = $original | Select-Object -ExpandProperty ServerName -Unique
    
    #Sort the dates (use Sort-Object -Descending for reverse order)
    $dates = $dates | ForEach-Object {Get-Date -Date $_} | Sort-Object | Get-Date -Format MMM-yy
    
    [psobject[]]$newFormat = $null
    $newFormat = foreach ($server in $servers)
    {
        Write-Verbose -Message "Adding $server"
        $object = New-Object -TypeName PSObject
        Add-Member -Name ServerName -MemberType NoteProperty -Value $server -InputObject $object
        foreach ($date in $dates)
        {
            Write-Verbose -Message "Finding average value for $date and server $server"
            Add-Member -InputObject $object -Name $date -MemberType NoteProperty -Value $($original | Where-Object{$_.ServerName -eq $server -and $_.Date -eq $date} | Select-Object -ExpandProperty Average)
        }
        $object
    }
    
    $newFormat | ft
    
    Assuming your object is also called original you obviously don't need the Import-CSV.

    Furthermore, does anyone know what this operation is called? Is this a pivot table?
     
    Last edited: Mar 8, 2017
  20. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,610
    I don't know if this is what a Pivot Table is, I chucked the Data into Excel and pressed the pivot table button, but it didn't do what I wanted :)... That was the extent of my Excel Skillz.

    I think what he is after, is a Time Series, for which Powershell probably isn't the tool for the job, but Google turns up a bit around it.

    https://github.com/openxtra/TimeTag
    https://hodgkins.io/using-powershell-to-send-metrics-graphite

    I came up with similar ways, but felt terrible while doing it, (not just because I was using a file instead of a Custom Object, but because the whole time I was doing it, something felt wrong...

    Hopefully some database person comes in and tells us how stupid we are for doing it this way. - So I'm cross posting the OP's question in the Programming sub forum to find out.

    Code:
    $MyStats = Import-Csv "C:\Scripts\Temp\Data.csv"
    
    #Get List of Unique Dates
    $ThisIsBad = $($MyStats | select Date -Unique ).date -join ","
    
    #Create Our CSV with headers
    Write-Output "ServerName,$ThisIsBad" | Out-File "C:\Scripts\Temp\ThisIsBad.Csv"
    
    #Get Servers
    $UniqueServers = $MyStats | Select ServerName -Unique
    foreach ($Server in $UniqueServers)
    	
    	{
    	 	$ThisIsRealBad = $($MyStats | Where {$_.ServerName -eq $Server.ServerName} | Sort-Object Date | Select Average).average -join ","
    		$BadString = $Server.ServerName + "," + $ThisIsRealBad
    		$BadString | Out-File "C:\Scripts\Temp\ThisIsBad.Csv" -Append
    	}
    	
    $BadDataisBad = Import-Csv "C:\Scripts\Temp\thisisbad.csv"
    $BadDataisBad 
    
     
    Last edited: Mar 8, 2017

Share This Page

Advertisement: