Tutorials:PowerShell Cmdlets: Difference between revisions
DoctorDalek (talk | contribs) No edit summary |
DoctorDalek (talk | contribs) |
||
Line 102: | Line 102: | ||
$Display = Get-PrimaryDisplay | $Display = Get-PrimaryDisplay | ||
Write-Host "$($Display | Write-Host "$($Display.Width)x$($Display.Height) @ $($Display.RefreshRate)Hz" | ||
1920x1080 | 1920x1080 @ 120Hz | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 23:47, 4 July 2024
When LANCommander executes PowerShell scripts through the Playnite addon, it imports a custom PowerShell module that adds useful cmdlets that can be used in your scripts. Examples of these are included under the "Functions" section of the LANCommander script editor. This page should be used as the official documentation of the cmdlets and their parameters.
Convert-AspectRatio
Calculates a resolution for the desired aspect ratio using an input display width and height.
Syntax
Convert-AspectRatio
-Width <int>
-Height <int>
-AspectRatio <double>
Description
The Convert-AspectRatio
cmdlet is most useful for calculating a resolution for a specific aspect ratio that will fit within a display by either using pillar or letter boxing. For example, some games may only support 4:3 displays and you may want to calculate the correct 4:3 resolution from your 16:9 display. This cmdlet is really useful when paired with Get-PrimaryDisplay
.
Examples
Convert-AspectRatio -Width 2560 -Height 1440 -AspectRatio (4 / 3)
# Returns <DisplayResolution>
Width : 1920
Height : 1440
ConvertTo-StringBytes
Converts an input string into a byte array.
Syntax
ConvertTo-StringBytes
-Input <string>
-Utf16 <bool>
-BigEndian <bool>
-MaxLength <int>
-MinLength <int>
Description
ConvertTo-StringBytes
is extremely useful for patching strings in binary files. It will take any input string and convert it to a byte array. Length can be controlled using the -MaxLength
and -MinLength
parameters.
Examples
ConvertTo-StringBytes -Input "Hello, world!" -Utf16 1
72 0 101 0 108 0 108 0 111 0 44 0 32 0 119 0 111 0 114 0 108 0 100 0 33 0
ConvertTo-StringBytes -Input "Hello, world!" -MaxLength
72 101 108 108 111
ConvertTo-StringBytes -Input "Hello" -MaxLength 10 -MinLength 10
72 101 108 108 111 0 0 0 0 0
ConvertTo-StringBytes -Input "Hello" -Utf16 1 -BigEndian 1
0 72 0 101 0 108 0 108 0 111
Edit-PatchBinary
Patches binary files at a specified offset.
Syntax
Edit-PatchBinary
-Offset <long>
-Data <byte[]>
-FilePath <string>
-MaxLength <int>
-MinLength <int>
Description
This cmdlet is useful when a binary file has to be patched at a specific offset. It can be extremely useful when paired with ConvertTo-StringBytes
to update a player name in a binary file.
Examples
$bytes = ConvertTo-StringBytes -Input "Master Chief" -Utf16 1 -MaxLength 16 -MinLength 16
Edit-PatchBinary -FilePath "$($env:LOCALAPPDATA)\Microsoft\Halo 2\Saved Games\S0000000\profile" -Offset 0x08 -Data $bytes
Get-GameManifest
Parses a game's manifest YAML file from the specified install directory
Syntax
Get-GameManifest
-Path <string>
Description
Used to deserialize a game's manifest file (_manifest.yml
) from the specified install directory. Returns the game manifest as an object.
Examples
$manifest = Get-GameManifest -Path "C:\Games\Age of Empires II - The Age of Kings"
Write-Host $manifest.Title
Age of Empires II: The Age of Kings
Get-PrimaryDisplay
Gets the bounds of the machine's current primary display.
Syntax
Get-PrimaryDisplay
Description
The Get-PrimaryDisplay
cmdlet takes no parameters and will only return the bounds of the current primary display attached to the machine. This is highly useful in where you might want to automatically set the game's resolution to match the primary display's resolution.
Examples
$Display = Get-PrimaryDisplay
Write-Host "$($Display.Width)x$($Display.Height) @ $($Display.RefreshRate)Hz"
1920x1080 @ 120Hz
Install-Game
Installs a game using the game ID. Requires an active connection to the LANCommander server.
Syntax
Install-Game
-Client <LANCommander.SDK.Client>
-Id <Guid>
-InstallDirectory <string>
Description
This cmdlet allows external PowerShell scripts to install games to the system. This is really only useful for admins that may want to install games from a remote PowerShell connection. Note that this requires an authenticated client. -InstallDirectory
will default to the path C:\Games
.
Examples
Import-Module "LANCommander.PowerShell.psd1"
$client = [LANCommander.SDK.Client]::new("http://<server>:1337")
$client.Authenticate("username", "password")
Install-Game -Client $client -Id <GameId> -InstallDirectory "C:\Games"
Uninstall-Game
Uninstalls a game at the given installation directory.
Syntax
Install-Game
-InstallDirectory <string>
Description
This cmdlet will run any uninstall scripts for a game and then delete the installation directory.
Examples
Uninstall-Game -InstallDirectory "C:\Games\Halo"
Write-GameManifest
Updates the value of an INI file
Syntax
Update-IniValue
-Section <string>
-Key <string>
-Value <string>
-FilePath <string>
-WrapValueInQuotes <bool> (optional)
Description
Update-IniValue
should be used when updating the values of an INI file. These files are typically used for configuring games and may be hard to edit using Write-ReplaceContentInFile
and regular expressions. INI files are comprised of sections (text surrounded in square brackets, e.g. [Display]
), and key-value pairs (e.g. Width=1024
).
Examples
# Change the resolution
$Display = Get-PrimaryDisplay
Update-IniValue -Section "Display" -Key "Width" -Value "$($Display.Width)" -FilePath "$InstallDirectory\config.ini"
Write-GameManifest
Serializes a GameManifest
object and writes it to disk.
Syntax
Write-GameManifest
-Path <string>
-Manifest <LANCommander.SDK.GameManifest>
Write-ReplaceContentInFile
Find and replace a string in a text file.
Syntax
Write-ReplaceContentInFile
-Pattern <string>
-Substitution <string>
-FilePath <string>
Description
Write-ReplaceContentInFile
can be used when you want to edit a text file and replace content. The -Pattern
parameter accepts regular expressions.
Examples
# Changes the player's multiplayer name in Call of Duty (2003)
Write-ReplaceContentInFile -Pattern '^seta name (.+)' -Substitution "seta name ""$NewPlayerAlias""" -FilePath "$InstallDirectory\Main\config_mp.cfg"