descargar_actualizar_y_ejecutar_script

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:

  1. Get-Content is 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.
  2. -replace is used to replace the placeholders in the script with your local variables.
  3. Set-Content is 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.

LOCAL_TOKEN_PLACEHOLDER and HOSTNAME_PLACEHOLDER would be placeholders in your script.ps1 file that you intend to replace with actual values.

Here's a short example of what your script.ps1 might look like before and after replacement:

Before Replacement:

# script.ps1
$myToken = "LOCAL_TOKEN_PLACEHOLDER"
$myHostname = "HOSTNAME_PLACEHOLDER"
 
# Rest of your script...

After Replacement:

# script.ps1
$myToken = "blahblahblah1234"
$myHostname = "my-computer-name"
 
# Rest of your script...

In this example, LOCAL_TOKEN_PLACEHOLDER and HOSTNAME_PLACEHOLDER are replaced with “blahblahblah1234” and “my-computer-name” respectively.

The -replace operation in PowerShell replaces all occurrences of the specified string value. So, if you have multiple occurrences of LOCAL_TOKEN_PLACEHOLDER or HOSTNAME_PLACEHOLDER in your script, they will all be replaced.

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.txt · Last modified: 2024/10/17 21:42 by 127.0.0.1