Powershell script does not execute in Linux

This topic contains 2 replies, has 3 voices, and was last updated by  js 2 weeks, 5 days ago.

  • Author
    Posts
  • #97944

    anita
    Participant

    I have installed powershell for Unix and am trying to run my script: Check-Folder.ps1
    ———
    function Check_Folder([string]$path, [switch]$create){
    $exists = Test-Path $path
    if (!$exists -and $create) {
    mkdir $path
    $exists = Test-Path $path
    }
    return $exists
    }
    —————
    In the unix command prompt, I run:
    > powershell -File ./Check-Folder.ps1 “/usr/bin”
    It returns without any output.
    Why?

  • #97956

    Joel Sallow
    Participant

    You've enclosed your code in a function. In order to execute the function, the script must call it at the end, or you need to 'import' the function from the file using dot-sourcing:

    . './Check-Folder.ps1'
    Check_Folder -Path '/usr/bin'

    I would generally recommend you use hyphens rather than underscores in function names, but it's up to you really. 🙂

  • #97965

    js
    Participant

    You can do it this way. If you make a directory though, it will return the directory as well.

    param([string]$path,[switch]$create)
    
    $exists = Test-Path $path
    if (!$exists -and $create) {
      mkdir $path
      $exists = Test-Path $path
    }
    return $exists
    

You must be logged in to reply to this topic.