diff --git a/src/cmd/graph_pwsh/Dockerfile b/src/cmd/graph_pwsh/Dockerfile index 24eabd647..2d83cb043 100644 --- a/src/cmd/graph_pwsh/Dockerfile +++ b/src/cmd/graph_pwsh/Dockerfile @@ -3,6 +3,11 @@ from m365pnp/powershell:2.1.1-alpine-3.14 RUN Install-Module PowerShellGet -Force RUN Install-Module Microsoft.Graph -Force -RequiredVersion 1.25.0 -Scope AllUsers +COPY ./Execute-Script.ps1 /tpm/Execute-Script.ps1 +RUN Move-Item -Path /tpm/Execute-Script.ps1 -Destination $ENV:PATH.Split(":")[0] + +# If you add other powershell commands this needs to be last since it sets +# the profile to run auth for the user which will then try to run for subsequent connads COPY ./Auth-Graph.ps1 /tmp/Auth-Graph.ps1 RUN Move-Item -Path /tmp/Auth-Graph.ps1 -Destination $PROFILE.AllUsersAllHosts diff --git a/src/cmd/graph_pwsh/Execute-Script.ps1 b/src/cmd/graph_pwsh/Execute-Script.ps1 new file mode 100644 index 000000000..5855bdb21 --- /dev/null +++ b/src/cmd/graph_pwsh/Execute-Script.ps1 @@ -0,0 +1,15 @@ +[CmdletBinding()] + +Param ( + [Parameter(Mandatory = $True, HelpMessage = "Powershell script URL")] + [string]$ScriptURL +) + +Invoke-WebRequest -Uri $ScriptURL -OutFile ./script.ps1 + +Write-Host "Executing the following script" +Write-Host "==============================" +cat ./script.ps1 +Write-Host "==============================" + +./script.ps1 \ No newline at end of file diff --git a/src/cmd/graph_pwsh/README.md b/src/cmd/graph_pwsh/README.md index 276353d35..b34a877b5 100644 --- a/src/cmd/graph_pwsh/README.md +++ b/src/cmd/graph_pwsh/README.md @@ -6,7 +6,7 @@ their environment. One convenient mechanism to accomplish this without going down to the level of wrapping individual Graph API calls is to use the -[Microsoft Graph PowerShell](https://learn.microsoft.com/en-us/powershell/microsoftgraph/overview?view=graph-powershell-1.0). +[Microsoft Graph Powershell](https://learn.microsoft.com/en-us/powershell/microsoftgraph/overview?view=graph-powershell-1.0). It provides a convenient wrapper and great coverage of the API surface. ## Build container @@ -17,6 +17,8 @@ Before using the tool you want to build the container that packages it. docker build -t corso/graph_pwsh:latest . ``` +A prebuilt image is also available currently available as `gmatev/graph_pwsh`. + ## Prerequisites ### Docker @@ -82,10 +84,25 @@ docker run --rm -it -v $(pwd):/usr/pwsh --env-file env_names corso/graph_pwsh \ pwsh -c "Get-MgUserMessage -UserId -MessageID " ``` +### Running a remote script + +In some cases, it may be prudent to run a more complex Powershell script that strings +a number of commands together. While it is possible to compact the sctipt to a single line, +that is quite inconvenient. + +Instead, it is possible to stash the desired script somewhere publically accessible (e.g. Gist) +and execute the container which will pull it and then execute it. This can be done using the +`Execute-Script.ps1` command as follows. + +```sh +docker run --rm -it -v $(pwd):/usr/pwsh --env-file env_names corso/graph_pwsh \ + pwsh -c "Execute-Script.ps1 -ScriptUrl