none
Calling install from Sub directories in Powershell RRS feed

  • Question

  • I am basically wanting to replicate %~dp0 from batch.

    This is what I have now but it errors out running from SCCM

    $ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path -Parent
    
    $ScriptDir\NetFramework452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe /norestart /q /log $LogDir\NET_452_Install.log


    Is my syntax wrong?


    Monday, March 13, 2017 7:09 PM

Answers

  • This should work:
    $ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
    & $ScriptDir\NetFramework452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe /norestart /q /log $LogDir\NET_452_Install.log
    

    Of course, $LogDir should also be defined, or else the log path will be the root (if $LogDir is empty).

    Get a copy of showargs.exe from this article:

    Windows IT Pro - Running Executables in PowerShell

    It will show you the actual command line that PowerShell constructs.


    -- Bill Stewart [Bill_Stewart]


    Monday, March 13, 2017 8:00 PM
    Moderator

All replies

  • $PSScriptRoot


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Monday, March 13, 2017 7:24 PM
  • Need this to work on PS version 2 that command is PS3+

    I think this works fine for script directory

    $ScriptDir = split-path -parent $MyInvocation.MyCommand.Definition

    But I think the script is grabbing the arguments and such I think it something to do with syntax but I can't see it.

    Monday, March 13, 2017 7:29 PM
  • This should work:
    $ScriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent
    & $ScriptDir\NetFramework452\NDP452-KB2901907-x86-x64-AllOS-ENU.exe /norestart /q /log $LogDir\NET_452_Install.log
    

    Of course, $LogDir should also be defined, or else the log path will be the root (if $LogDir is empty).

    Get a copy of showargs.exe from this article:

    Windows IT Pro - Running Executables in PowerShell

    It will show you the actual command line that PowerShell constructs.


    -- Bill Stewart [Bill_Stewart]


    Monday, March 13, 2017 8:00 PM
    Moderator
  • Okay I have this working but having issue calling an exe in from for Software updates... Example

    $ScriptDir = split-path -Path $MyInvocation.MyCommand.Definition -Parent

    wusa.exe & "$ScriptDir\KB3191565\Win7-x86\Win7-KB3191566-x86.msu" /quiet /norestart

    I get an error when trying to run this Apersand not allowed

    Wednesday, March 15, 2017 3:49 PM
  • That's because you put the & character is in the wrong place.

    You don't need it if the executable you're calling (or it's path) doesn't contain spaces.


    PS C:\> wusa.exe "$ScriptDir\KB3191565\Win7-x86\Win7-KB3191566-x86.msu" /quiet /norestart

    If the path or filename of the command you want to run contains spaces, then you would use the & to tell PowerShell: "The next string you see is a command to run, not a string."

    Read the article for help: Windows IT Pro - Running Executables in PowerShell


    -- Bill Stewart [Bill_Stewart]


    Wednesday, March 15, 2017 4:36 PM
    Moderator