Pablo's Powershell Pow-Wow

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

  1. Ogre

    Ogre Member

    Joined:
    Aug 13, 2003
    Messages:
    2,436
    Location:
    Sydney, Australia
    Moving the grabbing of scope and reservation info (Which does causes a progress bar to show) outside of the loop, has improved things nicely.

    Example of csv:
    Code:
    scopeid    Ipaddress    Name    ClientID    Description
    172.20.100.0    172.20.100.20    default    38-00-50-A8-1d-FF    Room 1 DIG SIGN
    172.20.150.0    172.20.150.100    default    00-01-2E-FF-B2-31    Room 1 VC
    172.20.150.0    172.20.150.101    default    00-01-FF-81-B2-32    Room 2 VC
    172.20.101.0    172.20.101.30    default    00-FF-1C-04-1d-76    Room 1 IPTV
    
     
  2. NSanity

    NSanity Member

    Joined:
    Mar 11, 2002
    Messages:
    17,476
    Location:
    Canberra

    If you needed more speed - you could simply build an array of jobs. or use foreach -parallel
     
  3. Ogre

    Ogre Member

    Joined:
    Aug 13, 2003
    Messages:
    2,436
    Location:
    Sydney, Australia
  4. millsy_c

    millsy_c Member

    Joined:
    Mar 31, 2007
    Messages:
    12,553
    Location:
    Brisbane
    Alright I'm stumped, got a script that's using $env:username as a variable.

    On a windows 10 machine, if I use say
    Code:
    powershell -command "Write-Host 'foo$env:username'"
    
    I'll get foousername in the response.

    If I do the same on a Windows 7 machine I'll get foo$env:username as a response.

    How on earth do I get earlier versions of powershell to behave the same? Unfortunately I can't wrap it in " instead of ' as it'll break -command.
     
  5. colmaz

    colmaz Member

    Joined:
    Jan 8, 2007
    Messages:
    410
    Location:
    Perth, WA
    Weird. On my Windows 10 PC I get foo$env:username.

    Can you try using backtics to escape the "?
    Code:
    powershell -command "Write-Host `"foo$env:username`""
    However, that doesn't work on my Win10 PC. I can use it without quotes altogether, or just quotes around the string
    Code:
    powershell -command Write-Host "foo$env:username"
    powershell -command Write-Host foo$env:username
    
     
  6. millsy_c

    millsy_c Member

    Joined:
    Mar 31, 2007
    Messages:
    12,553
    Location:
    Brisbane
    Nah backticks didn't work when I tried unfortunately. I'm sure it's just my misunderstanding on how to use the env vars in a script but yeah.
     
  7. Zedd02

    Zedd02 Member

    Joined:
    Oct 25, 2004
    Messages:
    1,128
    Location:
    Townsville
    How much module writing have people done in Powershell? I have a few questions regarding modules and things like help file locations, etc. Have the gurus done much in this regard?

    I'll ask the questions anyway, but don't think I need a definitive answer.

    1. When writing help files for modules, do they actually need to go in the \en-US\<help-file.xml> or do they go somewhere else? Do I have to reference them specially?
    2. I've broken my modules into multiple separate files (one for each function). What do I put in the root module file? So far it's just been an empty file. I suppose I could put basic text in there regarding the requirements for the module, etc.
    3. Should I be breaking my module into multiple separate files (one for each function) and then importing them using "Nested Module" or should I have them all in one file? How does that work with the given layout:
    Code:
    <#
    
      .Synopsis
    
      .Description
    
      .Parameter
    
      .Parameter
    
      .Example
    
      .Example
    
    #>
    Thanks all.
     
  8. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    12,929
    colmaz likes this.
  9. Zedd02

    Zedd02 Member

    Joined:
    Oct 25, 2004
    Messages:
    1,128
    Location:
    Townsville
    Holy crap. I've spent weeks looking for something like this. That was EXACTLY what I was looking for. It gives me 99% of the information I needed and all I need to do from here is expand it past a few things.
     
  10. kjparker

    kjparker Member

    Joined:
    Jun 28, 2001
    Messages:
    1,381
    Location:
    Sydney
    so I am in the process of updating our images for windows 10 build 1803, and I wanted to update our start layout, adding a few new tiles, moving them around etc, however when I go to export it as per the technet documentation, I get the following:
    Code:
    PS C:\WINDOWS\system32> Export-StartLayout -path C:\temp\layout.xml
    Export-StartLayout : Element not found.
    Element not found.
    At line:1 char:1
    + Export-StartLayout -path C:\temp\layout.xml
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Export-StartLayout], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.Windows.StartLayout.Commands.ExportStartLayoutCommand
    
    PS C:\WINDOWS\system32>
    
    Any ideas? Last time I went to change it would have been for 1607, and hadn't needed to touch it since. My google fu is failing me, and none of the solutions I have found online seem to work / be relevant...
     
  11. millsy_c

    millsy_c Member

    Joined:
    Mar 31, 2007
    Messages:
    12,553
    Location:
    Brisbane
    Forgot to update this, it was as simple as changing it from $env:username to %USERNAME% as the way the script was working it was the command prompt that was expanding the variables, not powershell.
     
  12. Dilbery

    Dilbery Member

    Joined:
    Nov 19, 2005
    Messages:
    1,034
    Location:
    Sydney, NSW
    Just tested on my 1803 build, works fine had to run powershell elevated though.
     
  13. kjparker

    kjparker Member

    Joined:
    Jun 28, 2001
    Messages:
    1,381
    Location:
    Sydney
    Strange.

    What flavour of 1803 do you have?

    Edit:
    Was previously running powershell as administrator, which wasnt working.

    I just logged into the computer with the local admin account, then ran powershell as admin again, and it worked. Strange!
     
    Last edited: May 9, 2018
  14. Dilbery

    Dilbery Member

    Joined:
    Nov 19, 2005
    Messages:
    1,034
    Location:
    Sydney, NSW
    1803 Enterprise, I've seen the start menu export do strange things before. Also, don't forget you can use the same XML to pin icons to the task bar.

    https://docs.microsoft.com/en-us/windows/configuration/configure-windows-10-taskbar
     
  15. qwertylesh

    qwertylesh Member

    Joined:
    Aug 21, 2007
    Messages:
    8,484
    work some self elevating code into your script, theyre easy to find, cobble together and work a charm. :)
     
  16. kjparker

    kjparker Member

    Joined:
    Jun 28, 2001
    Messages:
    1,381
    Location:
    Sydney
    If your referring to mine, it isnt really a script, just a one liner, and I was already running elevated. Dont know why I had to be logged into the machine as a local admin as well as being elevated, but if that's what I have to do, then so be it!
     
  17. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,397
    Location:
    Melbourne
    Hey Everyone,

    I have a weird issue with a script running in a scheduled task.

    I have setup logging on it, and it appears to be hanging on a particular command :
    Code:
     (get-HVlocalsession -Confirm:$false).NamesData
    When i run this in the ISE or powershell, it runs fine (with either my account or the one running the task)

    I have the task setup with the below command format, which work for all my other tasks

    Code:
    Program : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    Arguments : -executionpolicy bypass -noprofile -noninteractive -file <scriptfile>
    Start In : <Scriptpath>
    

    Any ideas on trying to work out what is happening
     
    Last edited: May 15, 2018
  18. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    12,929
    Divide and Conquer.

    does get-HVlocalsession return anything if run as a scheduled task by itself (not as part of a bigger script).
     
  19. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,397
    Location:
    Melbourne
    Good question

    I think it might be the import-csv actually holding the script, as the file its trying to import is 1.5 Gb

    just trying to work out if that is it
     
  20. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    12,929
    Now we are getting somewhere :).

    Processes launched from Scheduled Tasks run at a lower priority than processes started by users, for most scripts, it doesn't matter, but if you're smashing resources, it can.
    Export the Task, edit the priority in the XML, and then re-import it.
     
    Last edited: May 15, 2018

Share This Page