1. OCAU Merchandise is available! Check out our 20th Anniversary Mugs, Classic Logo Shirts and much more! Discussion in this thread.
    Dismiss Notice

Pablo's Powershell Pow-Wow

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

  1. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
  2. somebloke

    somebloke Member

    Joined:
    Feb 4, 2002
    Messages:
    217
    Location:
    Perth
    [​IMG]

    I think less than three you
     
  3. MrYoshi

    MrYoshi New Member

    Joined:
    Mar 23, 2015
    Messages:
    4
    thanks pablo!
     
  4. m0n4g3

    m0n4g3 Member

    Joined:
    Aug 5, 2009
    Messages:
    3,744
    Location:
    Perth, WA
    Couldn't agree more on the getting caught out and getting irrationally angry.
     
  5. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
    Literally 2 minutes after I posted that I had to check on some users who had Remote Desktop profles and home drives set...

    Not available via get-user...

    /rage :).
     
  6. EvilGenius

    EvilGenius Member

    Joined:
    Apr 26, 2005
    Messages:
    10,924
    Location:
    elsewhere
    Quick question, trying to search file and print servers for shared printers and export the results to CSV.

    $Printers = import-csv "c:\Servers.csv"
    foreach ($Printer in $Printers) {
    $Printer.Server
    Get-WmiObject -Class Win32_Printer -ComputerName $Printer.Server | Select SystemName, ShareName, Name, Location | Export-csv "F:\exportedPrinters.csv"
    }


    This searches each server in the CSV (I can see each server being scrolled through, errors for servers that are offline or whatever), creates the results CSV, but it only contains the results for the last server.


    Seems I'm telling it to run that search against each server and create that file for each server, so it's just overwriting it over and over again. Can't figure out how to write it so it collects all the results and outputs them though.
     
  7. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
    You are performing export-csv inside your loop, and each time it runs, it overwrites the output file.

    You can either build a new object inside your loop, and play with the output outside it, or (the quick and dirty way)

    is to use export-csv with the -append parameter.

    Code:
    Get-WmiObject -Class Win32_Printer -ComputerName $Printer.Server | Select SystemName, ShareName, Name, Location | Export-csv [B]-append[/B] "F:\exportedPrinters.csv"
     
  8. EvilGenius

    EvilGenius Member

    Joined:
    Apr 26, 2005
    Messages:
    10,924
    Location:
    elsewhere
    Seems there's no such thing as an append parameter?

    Export-Csv : A parameter cannot be found that matches parameter name 'append'.
     
  9. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
    Introduced in Powershell V3

    https://technet.microsoft.com/en-us/library/hh849932(v=wps.620).aspx

    Depending on what you want to do with it afterwards, you may be able to use Out-File instead of export-csv
     
  10. EvilGenius

    EvilGenius Member

    Joined:
    Apr 26, 2005
    Messages:
    10,924
    Location:
    elsewhere
    Oh well there we go.. Powershell V1.0 :rolleyes:

    Upgrading now.
     
  11. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
    The ComputerName parameter in Get-WmiObject can be a collection, so you don't need to fuck around with your own loops

    Code:
    $PrintServers = "PrintServer1","PrintServer2"
    Get-WmiObject -Class Win32_Printer -ComputerName $PrintServers | Select SystemName,ShareName,Name,Location | Export-Csv C:\Crap\printers.csv
    
     
  12. EvilGenius

    EvilGenius Member

    Joined:
    Apr 26, 2005
    Messages:
    10,924
    Location:
    elsewhere
    Append parameter worked just fine after the upgrade. New powershell is quite nice too, lists of available commands etc. I really need to practice this more.
     
  13. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,819
    Location:
    Brisbane
    I've very proudly created a simple script to do a full backup of our SQL databases, I now want to include them as part of our off-site backup. The problem is these SQL backups are approaching 2GB and the site only has 1.5Mbps upload.

    I have discovered powershell has a compress-archive cmdlet but from what i'm reading it has a 2GB filesize limitation.
    Has anybody worked around this filesize limitation and maybe has a better solution?
     
  14. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,819
    Location:
    Brisbane
    following on from the above I've found this that may work, I just don't know if its likely to have the same 2GB limitation as the cmdlet

    Code:
    Add-Type -assembly "system.io.compression.filesystem"
    $source = "c:\users\admin\desktop\test"
    $destination = "$source\test.zip"
    [io.compression.zipfile]::CreateFromDirectory($Source, $destination)
     
  15. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,678
    You'll need to use Start-Process or similar to run a native exe that has the ability to molest zip files bigger than 2GB.

    Any chance you could Sanitise and post your script? I've recently discovered the joys of Invoke-sqlcmd2 and how easy it makes writing glue to bring disparate systems inline.
     
  16. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,819
    Location:
    Brisbane
    Sure, the more people who pick it apart the less nasty I can make it :lol:

    Code:
    import-module sqlps
    cd SQL\$env:computername\DEFAULT\Databases
    
    $user = 'DOMAIN\GodUser'
    $pass = 'HisHoliness' | ConvertTo-SecureString -AsPlainText -Force
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
    $date = Get-Date -Format yyMMdd-HHmm
    
    mkdir "e:\NightlyBackup\$date"
    Backup-SqlDatabase -SqlCredential $cred -Database Heaven -BackupFile "e:\NightlyBackup\$date\$date.Heaven.bak" 
    Backup-SqlDatabase -SqlCredential $cred -Database Hell -BackupFile "e:\NightlyBackup\$date.Hell.bak" 
    
    Add-Type -assembly "system.io.compression.filesystem"
    $source = "e:\NightlyBackup\$date"
    $destination = "$source\$date.zip"
    [io.compression.zipfile]::CreateFromDirectory($Source, $destination) | out-null
    
    Remove-Item "e:\NightlyBackup\$date\$date.Heaven.bak"
    Remove-Item "e:\NightlyBackup\$date\$date.Hell.bak"
     
  17. karmic

    karmic Member

    Joined:
    Jun 28, 2001
    Messages:
    130
    Location:
    Perth
    I think you've done a great job with the overall structure. I would make the following changes (at the risk of turning a simple script into a slightly more complex one);
    1. Put paths into a variable, eg;
    $BackupPath=e:\NightlyBackup\$date
    mkdir $BackupPath

    2. Put DB names into an array, then target that array for the backup creation/copy/deletion processes, eg;
    $dbArray="Heaven","Hell"
    ForEach ($db in $dbArray) {Backup-SqlDatabase -SqlCredential $cred -Database $db -BackupFile "$BackupPath\$date\$date.$db.bak" }

    This gives you two easily modified areas that would then flow automatically to the reset of the script next time you want to reuse on another server or backup additional DB's etc.

    Kyle
     
  18. Embercide

    Embercide Member

    Joined:
    Jun 17, 2002
    Messages:
    1,819
    Location:
    Brisbane
    Excellent :thumbup: I'm still relatively new to arrays and i'm starting to amass a collection of scripts for each system whereas this will give me a 'universal' script. Cheers!
     
    Last edited: Nov 19, 2015
  19. kjparker

    kjparker Member

    Joined:
    Jun 28, 2001
    Messages:
    1,579
    Location:
    Sydney
    ok. I have one that's doing my head in and I cant figure out what the problem is.

    We are in an exchange 2010 environment, Hosted in head office overseas, and with Domain controllers distributed around the globe (each contry has their own local dc's and subdomain).

    EG:
    Root domain = company.net
    Local domain = au.company.net (replace au with prefix for country you are in)

    I am building a script to automate the creation of room resources. I am able to create the room object, however due to the AD / Exchange latency I need to wait a period of time (around 15 minutes) to set the calendar processing options.

    I found a workaround that basically created a loop of get-calendarprocessing waiting for it to return valid data before continuing onto the set-calendarprocessing section, however I dont seem to be able to use that command, even on a mailbox I am able to "set" them on.

    eg This command is sucessfull in setting the options

    Set-CalendarProcessing ROOM1 -AutomateProcessing AutoAccept -domaincontroller dc1.au.company.net

    however if I try and read from that same mailbox using the following command:

    get-calendarprocessing -Identity "Meeting ROOM1"

    I get the following:
    The operation couldn't be performed because object 'Meeting Room1' couldn't be found on 'ads004.company.net'.
    + CategoryInfo : NotSpecified: :)) [Get-CalendarProcessing], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 8BD18C4C,Microsoft.Exchange.Management.StoreTasks.GetCalendarProcessing
    + PSComputerName : exch01.company.net

    if I append the "-domaincontroller" option it throws the error below:

    A positional parameter cannot be found that accepts argument '-DomainController'.

    Any ideas?

    I dont believe this to be a permissions based issue as I can set the options, I suspect I am doing something wrong with my syntax, but I can't figure out what! I really want to get this figured out so that I can move onto repairing this head shaped hole in my desk!
     
  20. Chesto

    Chesto Member

    Joined:
    Sep 28, 2012
    Messages:
    34
    Your syntax looks good to me. I just tested using both alias and display name for one of my meeting room calendars and no issues (although this is just a single domain, single company setup).

    The error states exactly what is happening. If can't find the object on 'ads004.company.net', which says it's trying to query a DC not in the AU sub domain, and it just doesn't know anything about that object yet.

    Work around so that you can specify a DC to query for Get-CalendarProcessing would be:

    Code:
    Get-Mailbox "Meeting ROOM1" -DomainController dc1.au.xxxxxxx | Get-CalendarProcessing
     

Share This Page

Advertisement: