You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creates a web server that will invoke PowerShell code based on routes being asked for by the client.
@@ -9,9 +9,9 @@ Function New-PSWebServer {
Under the covers, New-PSWebServer uses the HTTPListener .NET class to execute powershell code as requested, retrieves the results and sends data back through the httplistener web server framework.
.ParameterUrl
Specifies a url/port in the form: http://servername:xxx to listen on where xxx is the port number to listen on. When specifying localhost with the public switch activated, it will enable listening on all IP addresses.
Specifies a url/port in the form: http://servername:xxx/ to listen on where xxx is the port number to listen on. When specifying localhost with the public switch activated, it will enable listening on all IP addresses.
.ParameterWebschema
Webschema takes a collection of hashes. Each element in the hash represents a different route or static content requested by the client. For routes, the three values used in the hash are path, method, and script. These hashes are abstracted by a DSL that you may use to build the hash. For static content, the values are path, source, and type where type can be either 'staticfile' or 'staticdirectory'
Webschema takes a collection of hashes. Each element in the hash represents a different route requested by the client. For routes, the three values used in the hash are path, method, and script. These hashes are abstracted by a DSL that you may use to build the hash.
method defines the HTTP method that will be used by the client to get to the route.
path defines the address in the url supplied by the client after the http://host:port/ part of the address. Paths support parameters allowed by Nancy. For example, if you your path is /process/{name}, the value supplied by the requestor for {name} is passed to your script. You would use the $parameters special variable to access the name property. In the /process/{name} example, the property would be $parameters.name in your script.
script is a scriptblock that will be executed when the client requests the path. The code will be routed to this scriptblock. The scriptblock has a special variable named $parameters that will accept client parameters. It also contains a $request special variable that contains the request info made by the client. The $request variable can be used to read post data from the client with the following example:
Get '/process/{name}' { get-process $parameters.name |convertto-json -depth 1 }
Get '/prettyprocess' { Get-Process | ConvertTo-HTML name, id, path }
staticfile '/index.html' '/content/index.html'
staticdirectory '/content' '/content/files'
)
Here is an example of the raw data that the above DSL creates. This may also be passed to -webschema:
$webschema = @(
@@ -60,93 +58,102 @@ Function New-PSWebServer {
script = {
Get-Process | ConvertTo-HTML name, id, path
}
},@{
path = '/index.html'
source = '/content/index.html'
type = 'staticfile'
},@{
path = '/content'
source = '/content/files'
type = 'staticdirectory'
}
)
.ParameterPath
This parameter runs the PSWebServer web server in that directory.
This is a useful parameter when using staticfile or staticdirectory in your webschema.
For example, if -path is set to c:\content, then staticfile /index.html /stuff.html would serve c:\content\stuff.html when a request is made for /index.html
By default, PSWebServer will set Path to be your current directory.
Path should either be an empty directory or a directory serving up static content. You will receive an access denied error if you try to use a Path that has hidden directories or links that would give you an error if you ran get-childitem -force. For example, this is common in c:\users\username\documents or root drives such as c:\ or d:\. Note: By default when running PSWebServer in a job, you will see this error because jobs start in c:\users\username\documents. You must use the -Path parameter to get around this error when running PSWebServer in a job.
.ParameterPublic
This allows you to use have your web server use a hostname other than localhost. Assuming your firewall is configured correctly, you will be able to serve the web calls over a network.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
If you have already run your own netsh command, it will not create a new one. For example, if you want to serve on http://server1:8000 with your service account named "PSWebServerservice", you could run netsh as follows instead of allowing New-PSWebServer to create a "+:8000 user=everyone" urlacl.
Returns the PSWebServer object. This is generally not needed by the other cmdlets.
.ParameterAuthenticationScheme
This is the authentication scheme or schemes your app will require. Default is anonymous.
.Inputs
Collection of hashes containing the schema of the web server
.Outputs
A Web server
.Example
New-PSWebServer
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be unreachable from outside of the server it is running on.
PS C:\> New-PSWebServer
Creates a web server listening on http://localhost:8000/. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be unreachable from outside of the server it is running on.
.Example
New-PSWebServer -Public
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be reachable from outside of the server it is running on.
PS C:\> New-PSWebServer -Public
Creates a web server listening on http://localhost:8000/. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be reachable from outside of the server it is running on.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
Get '/process' { get-process |select name, id, path |ConvertTo-Json }
Get '/prettyprocess' { Get-Process |ConvertTo-HTML name, id, path }
)
The above illustrates how you can set up multiple paths in a PSWebServer project. It also illustrates how to return text, create a web service that returns JSON, and display HTML visually.
The above creates three routes that can be accessed by a client (run on the server this was run on because the public switch was not used):
Get '/startprocessbyparameter/{name}' { start-process $parameters.name }
Get '/startprocessbyparameter/{name}' { start-process $parameters.name -PassThru | ConvertTo-HTML }
)
The above illustrates how the special variables $request and $parameters can be used in a scriptblock. The above illustrates how you can start a web server that will start processes based on either the data sent in POST to the route or by leveraging the parameters in a get route.
The last example here shows the use of authentication and authorized roles for each route. The startprocessbypost method will only allow access if you are running the browser as administrator and thus will authenticate as being in the local administrators group.
$null=New-Event-SourceIdentifier ConsoleMessageEvents -MessageData "$($context.User.Identity.Name) is not in any of the following groups:`n$($CurrentRoute.AuthorizedGroups)"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-PSWebServer creates a web server. The web server is composed of a schema that defines the client's requests to routes where PowerShell code is executed.
Under the covers, New-PSWebServer uses the HTTPListener .NET class to execute powershell code as requested, retrieves the results and sends data back through the httplistener web server framework.
.ParameterUrl
Specifies a url/port in the form: http://servername:xxx to listen on where xxx is the port number to listen on. When specifying localhost with the public switch activated, it will enable listening on all IP addresses.
.ParameterWebschema
Webschema takes a collection of hashes. Each element in the hash represents a different route or static content requested by the client. For routes, the three values used in the hash are path, method, and script. These hashes are abstracted by a DSL that you may use to build the hash. For static content, the values are path, source, and type where type can be either 'staticfile' or 'staticdirectory'
method defines the HTTP method that will be used by the client to get to the route.
path defines the address in the url supplied by the client after the http://host:port/ part of the address. Paths support parameters allowed by Nancy. For example, if you your path is /process/{name}, the value supplied by the requestor for {name} is passed to your script. You would use the $parameters special variable to access the name property. In the /process/{name} example, the property would be $parameters.name in your script.
script is a scriptblock that will be executed when the client requests the path. The code will be routed to this scriptblock. The scriptblock has a special variable named $parameters that will accept client parameters. It also contains a $request special variable that contains the request info made by the client. The $request variable can be used to read post data from the client with the following example:
Here is an example of creating the webschema with the DSL:
$webschema = @(
Get '/' { "Welcome to PSWebServer!" }
@@ -37,10 +29,7 @@ Function New-PSWebServer {
staticfile '/index.html' '/content/index.html'
staticdirectory '/content' '/content/files'
)
Here is an example of the raw data that the above DSL creates. This may also be passed to -webschema:
$webschema = @(
@{
path = '/'
@@ -81,66 +70,45 @@ Function New-PSWebServer {
type = 'staticdirectory'
}
)
.ParameterPath
This parameter runs the PSWebServer web server in that directory.
This is a useful parameter when using staticfile or staticdirectory in your webschema.
For example, if -path is set to c:\content, then staticfile /index.html /stuff.html would serve c:\content\stuff.html when a request is made for /index.html
By default, PSWebServer will set Path to be your current directory.
Path should either be an empty directory or a directory serving up static content. You will receive an access denied error if you try to use a Path that has hidden directories or links that would give you an error if you ran get-childitem -force. For example, this is common in c:\users\username\documents or root drives such as c:\ or d:\. Note: By default when running PSWebServer in a job, you will see this error because jobs start in c:\users\username\documents. You must use the -Path parameter to get around this error when running PSWebServer in a job.
.ParameterPublic
This allows you to use have your web server use a hostname other than localhost. Assuming your firewall is configured correctly, you will be able to serve the web calls over a network.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
Returns the PSWebServer object. This is generally not needed by the other cmdlets.
.ParameterAuthenticationScheme
This is the authentication scheme or schemes your app will require. Default is anonymous.
.Inputs
Collection of hashes containing the schema of the web server
.Outputs
A Web server
.Example
New-PSWebServer
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be unreachable from outside of the server it is running on.
.Example
New-PSWebServer -Public
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be reachable from outside of the server it is running on.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
Get '/process' { get-process |select name, id, path |ConvertTo-Json }
Get '/prettyprocess' { Get-Process |ConvertTo-HTML name, id, path }
)
The above illustrates how you can set up multiple paths in a PSWebServer project. It also illustrates how to return text, create a web service that returns JSON, and display HTML visually.
The above creates three routes that can be accessed by a client (run on the server this was run on because the public switch was not used):
http://localhost/
http://localhost/process
@@ -154,13 +122,10 @@ Function New-PSWebServer {
}
Get '/startprocessbyparameter/{name}' { start-process $parameters.name }
)
The above illustrates how the special variables $request and $parameters can be used in a scriptblock. The above illustrates how you can start a web server that will start processes based on either the data sent in POST to the route or by leveraging the parameters in a get route.
The last example here shows the use of authentication and authorized roles for each route. The startprocessbypost method will only allow access if you are running the browser as administrator and thus will authenticate as being in the local administrators group.
.LINK
https://github.com/tiberriver256/PSWebServer/
#>
param(
[Parameter(Position=0)]
@@ -225,33 +187,35 @@ while ($listener.IsListening)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Creates a web server that will invoke PowerShell code based on routes being asked for by the client.
.Description
New-PSWebServer creates a web server. The web server is composed of a schema that defines the client's requests to routes where PowerShell code is executed.
Under the covers, New-PSWebServer uses the HTTPListener .NET class to execute powershell code as requested, retrieves the results and sends data back through the httplistener web server framework.
.Parameter Url
Specifies a url/port in the form: http://servername:xxx to listen on where xxx is the port number to listen on. When specifying localhost with the public switch activated, it will enable listening on all IP addresses.
.Parameter Webschema
Webschema takes a collection of hashes. Each element in the hash represents a different route or static content requested by the client. For routes, the three values used in the hash are path, method, and script. These hashes are abstracted by a DSL that you may use to build the hash. For static content, the values are path, source, and type where type can be either 'staticfile' or 'staticdirectory'
method defines the HTTP method that will be used by the client to get to the route.
path defines the address in the url supplied by the client after the http://host:port/ part of the address. Paths support parameters allowed by Nancy. For example, if you your path is /process/{name}, the value supplied by the requestor for {name} is passed to your script. You would use the $parameters special variable to access the name property. In the /process/{name} example, the property would be $parameters.name in your script.
script is a scriptblock that will be executed when the client requests the path. The code will be routed to this scriptblock. The scriptblock has a special variable named $parameters that will accept client parameters. It also contains a $request special variable that contains the request info made by the client. The $request variable can be used to read post data from the client with the following example:
This parameter runs the PSWebServer web server in that directory.
This is a useful parameter when using staticfile or staticdirectory in your webschema.
For example, if -path is set to c:\content, then staticfile /index.html /stuff.html would serve c:\content\stuff.html when a request is made for /index.html
By default, PSWebServer will set Path to be your current directory.
Path should either be an empty directory or a directory serving up static content. You will receive an access denied error if you try to use a Path that has hidden directories or links that would give you an error if you ran get-childitem -force. For example, this is common in c:\users\username\documents or root drives such as c:\ or d:\. Note: By default when running PSWebServer in a job, you will see this error because jobs start in c:\users\username\documents. You must use the -Path parameter to get around this error when running PSWebServer in a job.
.Parameter Public
This allows you to use have your web server use a hostname other than localhost. Assuming your firewall is configured correctly, you will be able to serve the web calls over a network.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
Returns the PSWebServer object. This is generally not needed by the other cmdlets.
.Parameter AuthenticationScheme
This is the authentication scheme or schemes your app will require. Default is anonymous.
.Inputs
Collection of hashes containing the schema of the web server
.Outputs
A Web server
.Example
New-PSWebServer
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be unreachable from outside of the server it is running on.
.Example
New-PSWebServer -Public
Creates a web server listening on http://localhost:8000. The server will respond with "Hello World!" when http://localhost:8000 is browsed to. The server will be reachable from outside of the server it is running on.
This will require admin privileges to run. If you do not have admin privs, a prompt will ask you if you would like to elevate. If you choose to do this, the server will have the following run as admin. This will allow users to serve on port 8000 from this server:
Get '/process' { get-process |select name, id, path |ConvertTo-Json }
Get '/prettyprocess' { Get-Process |ConvertTo-HTML name, id, path }
)
The above illustrates how you can set up multiple paths in a PSWebServer project. It also illustrates how to return text, create a web service that returns JSON, and display HTML visually.
The above creates three routes that can be accessed by a client (run on the server this was run on because the public switch was not used):
Get '/startprocessbyparameter/{name}' { start-process $parameters.name }
)
The above illustrates how the special variables $request and $parameters can be used in a scriptblock. The above illustrates how you can start a web server that will start processes based on either the data sent in POST to the route or by leveraging the parameters in a get route.
The last example here shows the use of authentication and authorized roles for each route. The startprocessbypost method will only allow access if you are running the browser as administrator and thus will authenticate as being in the local administrators group.