I got a big problem. i need a bat script that will set the ipv4 on a computer based on its name. The hostname for example is cho-ryn-k1 and i need the script to take the three first octets from the current ipv4, and change the last one to 201 (k2 will have 202, k3 -> 203 etc) only the hostname ending with s1 will end in 10. i also need the script to set the mask to 255.255.255.0, set two dns's 8.8.8.8 and 1.1.1.1, set two gateways, both with the same three octets one ending in 254 second ending with 253.
NOW the problem is that in win11 (a system every computer i need to setup uses) i can only right click and run in powershell with no admin privileges. The script does not change any of the actual configuration. What I need is a way to run this as admin right away.
$hostname = hostname
$currentIP = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null -and $_.IPEnabled -eq $true }).IPAddress[0]
$firstThreeOctets = $currentIP -replace '\.\d+$', ''
if ($hostname -like "*s1") {
$desiredIP = "$firstThreeOctets.10"
} else {
$number = $hostname -replace '[^0-9]', ''
$desiredIP = "$firstThreeOctets.20$number"
}
$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null -and $_.IPEnabled -eq $true }
$adapter.EnableStatic($desiredIP, "255.255.255.0") | Out-Null
$dnsServers = "8.8.8.8", "1.1.1.1"
$adapter.SetDNSServerSearchOrder($dnsServers) | Out-Null
$gateway1 = "$firstThreeOctets.254"
$gateway2 = "$firstThreeOctets.253"
$gateways = @($gateway1, $gateway2)
$adapter.SetGateways($gateways) | Out-Null
Write-Host "Stworzono przez " -NoNewline
Write-Host "Maksymilian Gawron" -ForegroundColor Red -NoNewline
Write-Host " dla wdrozen aptek " -NoNewline
Write-Host "Bankleku" -ForegroundColor Blue
Write-Host "Nazwa komputera: " -NoNewline
Write-Host "$hostname" -ForegroundColor Green
Write-Host "Obecny adres: " -NoNewline
Write-Host "$desiredIP" -ForegroundColor Green
Write-Host "Maska: " -NoNewline
Write-Host "255.255.255.0" -ForegroundColor Green
Write-Host "Brama 1: " -NoNewline
Write-Host "$gateway1" -ForegroundColor Green
Write-Host "Brama 2: " -NoNewline
Write-Host "$gateway2" -ForegroundColor Green
Write-Host "DNSy: " -NoNewline
Write-Host "$dnsServers" -ForegroundColor Green
The only way now that i know this works is running powershell in admin and copying the whole code. I don't want to copy the code from the .ps1 file to the powershell. the purpose of the script is for it to save time. If you tell that i need to change a lot of things inside cmd or pwsh then the script is just useless. The point is i need to have a pendrive, go up to the PC, run the script and be done.