descargar_actualizar_y_ejecutar_script
This is an old revision of the document!
You can certainly use PowerShell to download a script from a remote location, update it with local variables, and then execute it. Here's an example of how you can do it:
# Declare your variables $localToken = "blahblahblah1234" $hostname = $(hostname) # Download the script Invoke-WebRequest -Uri https://mydomain.com/files/script.ps1 -OutFile C:\temp\script.ps1 # Update the script with your variables (Get-Content -Path C:\temp\script.ps1) -replace 'LOCAL_TOKEN_PLACEHOLDER', $localToken -replace 'HOSTNAME_PLACEHOLDER', $hostname | Set-Content -Path C:\temp\script.ps1 # Execute the updated script Invoke-Expression -Command C:\temp\script.ps1
In this script:
Get-Contentis a cmdlet that gets the content of the item at the location specified by the path, such as the text in a file. It reads the content one line at a time, returning an array of objects each of which represents a line of content.-replaceis used to replace the placeholders in the script with your local variables.Set-Contentis a cmdlet that writes or replaces the content in an item with new content.
Please replace LOCAL_TOKEN_PLACEHOLDER and HOSTNAME_PLACEHOLDER with the actual placeholders in your script.
Remember, as always, to only run scripts from trusted sources to avoid potential security risks. Let me know if you need further assistance! 😊.
descargar_actualizar_y_ejecutar_script.1705937745.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
