# PowerShell

### Module

Per Standard für Salt Kommandos, welche mit `cmd.run` „losgeschickt“ werden mit `cmd.exe` aus. Sie können entweder aus cmd.exe PowerShell aufrufen, oder die Shell Variable ändern. Siehe Beispiele:

```bash
salt '*' cmd.run 'powershell -Command "Get-Date"'
salt '*' cmd.run 'Get-Date' shell=powershell
salt '*' cmd.powershell 'Get-Date'
salt '*' cmd.run '$(Get-ComputerInfo).CsModel' shell=powershell
```

<figure><img src="https://3666581685-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LFRZJPZX11pwB1staCp%2Fuploads%2Fw1fWtX83HSVYOXInSgvn%2Fimage.png?alt=media&#x26;token=a6676ea2-78e0-4c7d-8e16-266dc9081973" alt=""><figcaption><p>Verschiedene PowerShell Optionen</p></figcaption></figure>

### State

Wenn Sie in einem State ein PowerShell Script ausführen möchten, müssen Sie ebenfalls die Variable `shell` auf `powershell` setzen.

{% code title="/src/salt/windows/pow\.sls" %}

```yaml
script:
  file.managed:
    - name: 'C:\Windows\Temp\reboot.ps1'
    - contents: |
        sleep 10
        Restart-Computer -Force

reboot:
  cmd.run:
    - name: Start-Process -NoNewWindow -FilePath "powershell" -ArgumentList "reboot.ps1" -WorkingDirectory "C:\Windows\temp"
    - shell: powershell
    - env:
      - ExecutionPolicy: bypass
    - user: Administrator

```

{% endcode %}

### Mehrzeilige PowerShell Script in States

Wenn Sie einen State auf einem Linux-System erstellen, versteht die PowerShell den Zeilenumbruch nicht. Ein State wie der Nachfolgende würde nur das erste Kommando auf der PowerShell ausführen und die Restlichen ignorieren.

```yaml
script:
  cmd.run:
    - name: |
        Get-Date
        Write-Host "Hallo Salt"
        Get-Date
        Write-Host "Hallo Salt"
    - shell: powershell
```

Dieses Problem können Sie umschiffen, indem die mit dem Yaml-Operator `>-` "[Folded Blocks](https://redocly.com/docs/yaml/blocks-and-flows#folded-blocks)" alles auf eine Zeile bringen lassen und dann explizit das Semikolon zum Abtrennen der Kommandos angeben.

```yaml
script:
  cmd.run:
    - name: >-
        Get-Date;
        Write-Host "Hallo Salt";
        Get-Date;
        Write-Host "Hallo Salt";
    - shell: powershell
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thorstenkramm.gitbook.io/saltstack/salt-fuer-windows/powershell.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
