1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
$userName = "rsyncuser" $passwordPlainText = "YourStrongPasswordHere" $sshPubKey = "您的公钥内容" $rsyncDataDir = "C:\RsyncData"
Write-Host "===== 检查 rsyncuser 用户是否存在 ====="
$securePassword = ConvertTo-SecureString $passwordPlainText -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
$user = Get-WmiObject Win32_UserAccount -Filter "Name='$userName'"
if ($user -eq $null) { Write-Host "用户不存在,正在创建用户:$userName"
net user "$userName" $passwordPlainText /add
wmic UserAccount where "Name='$userName'" set PasswordExpires=FALSE
Write-Host "用户创建成功:$userName" Write-Host "正在以 $userName 身份静默登录以生成用户目录..." Start-Process -FilePath "cmd.exe" -ArgumentList "/c exit" -Credential $credential -WindowStyle Hidden -ErrorAction Stop Write-Host "静默登录完成。" } else { Write-Host "用户已存在:$userName" }
Write-Host "===== 创建 .ssh 目录和 authorized_keys 文件 =====" $sshDir = "C:\Users\$userName\.ssh" if (!(Test-Path -Path $sshDir)) { Write-Host "创建目录:$sshDir" New-Item -ItemType Directory -Path $sshDir -Force } else { Write-Host "目录已存在:$sshDir" }
$authorizedKeysFile = Join-Path $sshDir "authorized_keys" if (!(Test-Path -Path $authorizedKeysFile)) { Write-Host "创建文件:$authorizedKeysFile" New-Item -ItemType File -Path $authorizedKeysFile -Force Set-Content -Path $authorizedKeysFile -Value $sshPubKey } else { Write-Host "文件已存在:$authorizedKeysFile" }
Write-Host "===== 设置 .ssh 目录权限 =====" $acl = Get-Acl -Path $sshDir $acl.SetAccessRuleProtection($true, $false) $acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) }
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( $userName, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow" ) $acl.AddAccessRule($rule)
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( "SYSTEM", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow" ) $acl.AddAccessRule($rule)
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( "Administrators", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow" ) $acl.AddAccessRule($rule)
Set-Acl -Path $sshDir -AclObject $acl
Write-Host "===== 设置 authorized_keys 文件权限 =====" $acl = Get-Acl -Path $authorizedKeysFile $acl.SetAccessRuleProtection($true, $false) $acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) }
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( $userName, "Read", "None", "None", "Allow" ) $acl.AddAccessRule($rule)
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( "SYSTEM", "FullControl", "None", "None", "Allow" ) $acl.AddAccessRule($rule)
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( "Administrators", "FullControl", "None", "None", "Allow" ) $acl.AddAccessRule($rule)
Set-Acl -Path $authorizedKeysFile -AclObject $acl
Write-Host "===== 创建 RsyncData 目录并设置权限 ====="
if (!(Test-Path -Path $rsyncDataDir)) { Write-Host "创建目录:$rsyncDataDir" New-Item -Path $rsyncDataDir -ItemType Directory -Force } else { Write-Host "目录已存在:$rsyncDataDir" }
$Acl = Get-Acl -Path $rsyncDataDir $Acl.SetAccessRuleProtection($true, $false) $Acl.Access | ForEach-Object { $Acl.RemoveAccessRule($_) }
$AccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList @( $userName, "Modify", "ContainerInherit,ObjectInherit", "None", "Allow" ) $Acl.AddAccessRule($AccessRule)
Set-Acl -Path $rsyncDataDir -AclObject $Acl
Write-Host "===== 全部配置完成 ====="
|