Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@numbnet
Last active April 11, 2021 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numbnet/66b374caf67a6c61938b05a26d532f6e to your computer and use it in GitHub Desktop.
Save numbnet/66b374caf67a6c61938b05a26d532f6e to your computer and use it in GitHub Desktop.
Win32
#Install Hyper-V on Windows 10
## Enable Hyper-V with PowerShell
## powershell
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
## Enable Hyper-V with CMD and DISM
#DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
#################
## CREATE VHD DRIVE
## create FIXED VHD
# New-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx" -Fixed -SizeBytes 1GB
## create Dynamic VHDX
New-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx" -Dynamic -SizeBytes 512GB
####################
## CMD
## create vdisk file="Full path of .vhd or .vhdx location" maximum=<size> type=fixed
##
#create vdisk file="Full path of .vhd or .vhdx location" maximum=<size> type=expandable
#
## create vdisk file="D:\VHD\Example.vhdx" maximum=1024 type=fixed
#####################
#####################
diskpart
select vdisk file="$env:SYSTEMDRIVE\VHD\VHD.vhdx"
attach vdisk
create partition primary
format fs=NTFS label="FriendlyName" quick
assign letter=A
## Mount VHD
Mount-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx"
###################################
## UnMount VHD
echo" Off Vdisk"
pause
Dismount-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx"
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Get-Help New-VHD
##########################################3
pause
New-Item -Path $env:SYSTEMDRIVE\ -Name 'VHD' -ItemType Directory
$vhdpath = "$env:SYSTEMDRIVE\VHD\VHD.vhdx"
$vhdSectorSize = 512
$vhdBlockSize = 128MB
$vhdLogicalSectorSize = 4KB
$vhdsize = 512GB
New-VHD -Path $vhdpath -PhysicalSectorSizeBytes $vhdSectorSize -BlockSizeBytes $vhdBlockSize -LogicalSectorSize $vhdLogicalSectorSize -Dynamic -SizeBytes $vhdsize | Mount-VHD -Passthru |Initialize-Disk -Passthru |New-Partition -AssignDriveLetter -UseMaximumSize |Format-Volume -FileSystem NTFS -Confirm:$false -Force
#New-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx" -Dynamic -SizeBytes 512GB
pause
##########################################3
New-Item -Path $env:SYSTEMDRIVE\ -Name "PS\VHD" -ItemType Directory
New-Item -Path $env:SYSTEMDRIVE\ -Name "PS\VHD\CreatePartVHD.txt" -ItemType File
echo 'REM ## Partition VHD Drive' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'select vdisk file=$env:SYSTEMDRIVE\VHD\VHD.vhdx' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'convert gpt' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'attach vdisk' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'create partition primary' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'format fs=NTFS label="VHD" quick' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'assign letter=A' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
echo 'exit' >> $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
######################################
pause
DiskPart /s $env:SYSTEMDRIVE\PS\VHD\CreatePartVHD.txt
## Mount VHD
Mount-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx"
###################################
## UnMount VHD
echo" Off Vdisk"
pause
Dismount-VHD -Path "$env:SYSTEMDRIVE\VHD\VHD.vhdx"
pause
# wpeutil shutdown
##====================================================
## start as ADMIN
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}
echo "$env:COMPUTERNAME"
$CompName = NNL
Rename-Computer -NewName $CompName -Force
Restart-Computer -Confirm -Force
##*************************************************
New-Item -Path $env:SYSTEMDRIVE\ -Name 'PS\OpenSSH' -ItemType Directory
##====================================================
### ARH OS and Download OpenSSH x64 or x86
echo "******** Start Download OpenSSH v8.1.0.0p1-Beta .......... *********";
echo " "
if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
{
Write '... for Windows x64';
Invoke-WebRequest -Uri 'https://github.com/PowerShell/Win32-OpenSSH/releases/download/v8.1.0.0p1-Beta/OpenSSH-Win64.zip' -OutFile $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win64.zip
pause
##=====================================
## Unzip the files
Expand-Archive -Path $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win64.Zip -DestinationPath $env:ProgramFiles\OpenSSH
pause
##================= ≠≠≠ =================
echo '## Copy \OpenSSH\OpenSSH-Win64\ \OpenSSH'
copy $env:ProgramFiles\OpenSSH\OpenSSH-Win64\* $env:ProgramFiles\OpenSSH
} else {
Write "... for Windows x86"
Invoke-WebRequest -Uri 'https://github.com/PowerShell/Win32-OpenSSH/releases/download/v8.1.0.0p1-Beta/OpenSSH-Win32.zip' -OutFile $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win32.zip
pause
echo '## Unzip the files'
Expand-Archive -Path $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win32.Zip -DestinationPath $env:ProgramFiles\OpenSSH
echo '## Copy \OpenSSH\OpenSSH-Win64\ \OpenSSH'
copy $env:ProgramFiles\OpenSSH\OpenSSH-Win32\* $env:ProgramFiles\OpenSSH
}
##================= ≠≠≠ =================
echo '## Install service'
. $env:ProgramFiles\OpenSSH\install-sshd.ps1
##================= ≠≠≠ =================
echo '## Set firewall permissions'
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22;
##================= ≠≠≠ =================
echo '## Set service startup END'
Set-Service sshd -StartupType Automatic;
Start-Service sshd;
##================= ≠≠≠ =================
echo '## Set Authentication to public key'
((Get-Content -path $env:ProgramData\ssh\sshd_config -Raw) ` -replace '#PubkeyAuthentication yes','PubkeyAuthentication yes' ` -replace '#PasswordAuthentication yes','PasswordAuthentication yes' ` -replace 'Match Group administrators','#Match Group administrators' ` -replace 'AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys','#AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys') | Set-Content -Path $env:ProgramData\ssh\sshd_config;
##================= ≠≠≠ =================
echo '## Restart after changes'
Restart-Service sshd;
##================= ≠≠≠ =================
echo '## force file creation'
New-item -Path "$env:USERPROFILE" -Name .ssh -ItemType Directory -force;
##================= ≠≠≠ =================
echo '## Gen SSH-KEY:'
#ssh-keygen -t ed25519 -C "$env:USERNAME" -f $env:USERPROFILE\.ssh\$env:COMPUTERNAME.$(Get-Random).ed25519.key | Out-File $env:USERPROFILE\.ssh\file.log -Encoding ascii;
#ssh-keygen -t ed25519 -C "$env:USERNAME" -f "$env:USERPROFILE\.ssh\$env:COMPUTERNAME.$env:USERNAME.ed25519.key" | Out-File $env:USERPROFILE\.ssh\file.log -Encoding ascii;
##================= ≠≠≠ =================
echo '## Copy SSH-KEY V1:'
New-Item -Path $env:USERPROFILE\ -Name '.ssh' -ItemType Directory
New-Item -Path $env:USERPROFILE\ -Name '.ssh\authorized_keys' -ItemType file
cat $env:USERPROFILE\.ssh\*$env:COMPUTERNAME.$env:USERNAME.ed25519.key.pub | Out-File $env:USERPROFILE\.ssh\authorized_keys -Encoding ascii
#echo 'ssh-ed25519 AAAA************************************************************YyHsv' | Out-File $env:USERPROFILE\.ssh\authorized_keys -Encoding ascii
cat $env:USERPROFILE\.ssh\authorized_keys
## Copy SSH-KEY V2:
#cat $env:USERPROFILE\.ssh\*$env:COMPUTERNAME.$env:USERNAME.ed25519.key.pub | Out-File $env:USERPROFILE\.ssh\authorized_keys -Encoding ascii;
##================= ≠≠≠ =================
## Cleaning Dir
echo ### ОПРЕДЕЛЯЕМ АРХИТЕКТУРУ os Download OpenSSH x64 or x86
if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
{
Write 'Remove for Windows x64';
Remove-Item -Path "$env:ProgramFiles\OpenSSH\OpenSSH-Win64" -Recurse;
Remove-Item -Path $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win64.Zip -Recurse;
} else {
Write 'Remove for Windows x86';
Remove-Item -Path $env:ProgramFiles\OpenSSH\OpenSSH-Win32 -Recurse;
Remove-Item -Path $env:SYSTEMDRIVE\PS\OpenSSH\OpenSSH-Win32.zip -Recurse;
}
##================= ≠≠≠ =================
echo 'OpenSSH for windows is installed'
exit
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
echo 'running with full privileges'
pause
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment