k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/gce/windows/testonly/user-profile.psm1 (about) 1 2 <# 3 .Synopsis 4 Rough PS functions to create new user profiles 5 .DESCRIPTION 6 Call the Create-NewProfile function directly to create a new profile 7 .EXAMPLE 8 Create-NewProfile -Username 'testUser1' -Password 'testUser1' 9 .NOTES 10 Created by: Josh Rickard (@MS_dministrator) and Thom Schumacher (@driberif) 11 Forked by: @crshnbrn66, then @pjh (2018-11-08). See 12 https://gist.github.com/pjh/9753cd14400f4e3d4567f4553ba75f1d/revisions 13 Date: 24MAR2017 14 Location: https://gist.github.com/crshnbrn66/7e81bf20408c05ddb2b4fdf4498477d8 15 16 Contact: https://github.com/MSAdministrator 17 MSAdministrator.com 18 https://github.com/crshnbrn66 19 powershellposse.com 20 #> 21 22 # IMPORTANT PLEASE NOTE: 23 # Any time the file structure in the `windows` directory changes, `windows/BUILD` 24 # and `k8s.io/release/lib/releaselib.sh` must be manually updated with the changes. 25 # We HIGHLY recommend not changing the file structure, because consumers of 26 # Kubernetes releases depend on the release structure remaining stable. 27 28 29 #Function to create the new local user first 30 function New-LocalUser 31 { 32 [CmdletBinding()] 33 [Alias()] 34 [OutputType([int])] 35 Param 36 ( 37 # Param1 help description 38 [Parameter(Mandatory=$true, 39 ValueFromPipelineByPropertyName=$true, 40 Position=0)] 41 $userName, 42 # Param2 help description 43 [string] 44 $password 45 ) 46 47 $system = [ADSI]"WinNT://$env:COMPUTERNAME"; 48 $user = $system.Create("user",$userName); 49 $user.SetPassword($password); 50 $user.SetInfo(); 51 52 $flag=$user.UserFlags.value -bor 0x10000; 53 $user.put("userflags",$flag); 54 $user.SetInfo(); 55 56 $group = [ADSI]("WinNT://$env:COMPUTERNAME/Users"); 57 $group.PSBase.Invoke("Add", $user.PSBase.Path); 58 } 59 60 #function to register a native method 61 function Register-NativeMethod 62 { 63 [CmdletBinding()] 64 [Alias()] 65 [OutputType([int])] 66 Param 67 ( 68 # Param1 help description 69 [Parameter(Mandatory=$true, 70 ValueFromPipelineByPropertyName=$true, 71 Position=0)] 72 [string]$dll, 73 74 # Param2 help description 75 [Parameter(Mandatory=$true, 76 ValueFromPipelineByPropertyName=$true, 77 Position=1)] 78 [string] 79 $methodSignature 80 ) 81 82 $script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; } 83 } 84 function Get-Win32LastError 85 { 86 [CmdletBinding()] 87 [Alias()] 88 [OutputType([int])] 89 Param($typeName = 'LastError') 90 if (-not ([System.Management.Automation.PSTypeName]$typeName).Type) 91 { 92 $lasterrorCode = $script:lasterror | ForEach-Object{ 93 '[DllImport("kernel32.dll", SetLastError = true)] 94 public static extern uint GetLastError();' 95 } 96 Add-Type @" 97 using System; 98 using System.Text; 99 using System.Runtime.InteropServices; 100 public static class $typeName { 101 $lasterrorCode 102 } 103 "@ 104 } 105 } 106 #function to add native method 107 function Add-NativeMethods 108 { 109 [CmdletBinding()] 110 [Alias()] 111 [OutputType([int])] 112 Param($typeName = 'NativeMethods') 113 114 $nativeMethodsCode = $script:nativeMethods | ForEach-Object { " 115 [DllImport(`"$($_.Dll)`")] 116 public static extern $($_.Signature); 117 " } 118 119 Add-Type @" 120 using System; 121 using System.Text; 122 using System.Runtime.InteropServices; 123 public static class $typeName { 124 $nativeMethodsCode 125 } 126 "@ 127 } 128 129 #Main function to create the new user profile 130 function Create-NewProfile { 131 132 [CmdletBinding()] 133 [Alias()] 134 [OutputType([int])] 135 Param 136 ( 137 # Param1 help description 138 [Parameter(Mandatory=$true, 139 ValueFromPipelineByPropertyName=$true, 140 Position=0)] 141 [string]$UserName, 142 143 # Param2 help description 144 [Parameter(Mandatory=$true, 145 ValueFromPipelineByPropertyName=$true, 146 Position=1)] 147 [string] 148 $Password 149 ) 150 151 Write-Verbose "Creating local user $Username"; 152 153 try 154 { 155 New-LocalUser -username $UserName -password $Password; 156 } 157 catch 158 { 159 Write-Error $_.Exception.Message; 160 break; 161 } 162 $methodName = 'UserEnvCP' 163 $script:nativeMethods = @(); 164 165 if (-not ([System.Management.Automation.PSTypeName]$MethodName).Type) 166 { 167 Register-NativeMethod "userenv.dll" "int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,` 168 [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,` 169 [Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath, uint cchProfilePath)"; 170 171 Add-NativeMethods -typeName $MethodName; 172 } 173 174 $localUser = New-Object System.Security.Principal.NTAccount("$UserName"); 175 $userSID = $localUser.Translate([System.Security.Principal.SecurityIdentifier]); 176 $sb = new-object System.Text.StringBuilder(260); 177 $pathLen = $sb.Capacity; 178 179 Write-Verbose "Creating user profile for $Username"; 180 181 try 182 { 183 [UserEnvCP]::CreateProfile($userSID.Value, $Username, $sb, $pathLen) | Out-Null; 184 } 185 catch 186 { 187 Write-Error $_.Exception.Message; 188 break; 189 } 190 } 191 192 function New-ProfileFromSID { 193 194 [CmdletBinding()] 195 [Alias()] 196 [OutputType([int])] 197 Param 198 ( 199 # Param1 help description 200 [Parameter(Mandatory=$true, 201 ValueFromPipelineByPropertyName=$true, 202 Position=0)] 203 [string]$UserName, 204 [string]$domain = 'PHCORP' 205 ) 206 $methodname = 'UserEnvCP2' 207 $script:nativeMethods = @(); 208 209 if (-not ([System.Management.Automation.PSTypeName]$methodname).Type) 210 { 211 Register-NativeMethod "userenv.dll" "int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,` 212 [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,` 213 [Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath, uint cchProfilePath)"; 214 215 Add-NativeMethods -typeName $methodname; 216 } 217 218 $sb = new-object System.Text.StringBuilder(260); 219 $pathLen = $sb.Capacity; 220 221 Write-Verbose "Creating user profile for $Username"; 222 #$SID= ((get-aduser -id $UserName -ErrorAction Stop).sid.value) 223 if($domain) 224 { 225 $objUser = New-Object System.Security.Principal.NTAccount($domain, $UserName) 226 $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 227 $SID = $strSID.Value 228 } 229 else 230 { 231 $objUser = New-Object System.Security.Principal.NTAccount($UserName) 232 $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 233 $SID = $strSID.Value 234 } 235 Write-Verbose "$UserName SID: $SID" 236 try 237 { 238 $result = [UserEnvCP2]::CreateProfile($SID, $Username, $sb, $pathLen) 239 if($result -eq '-2147024713') 240 { 241 $status = "$userName already exists" 242 write-verbose "$username Creation Result: $result" 243 } 244 elseif($result -eq '-2147024809') 245 { 246 $staus = "$username Not Found" 247 write-verbose "$username creation result: $result" 248 } 249 elseif($result -eq 0) 250 { 251 $status = "$username Profile has been created" 252 write-verbose "$username Creation Result: $result" 253 } 254 else 255 { 256 $status = "$UserName unknown return result: $result" 257 } 258 } 259 catch 260 { 261 Write-Error $_.Exception.Message; 262 break; 263 } 264 $status 265 } 266 Function Remove-Profile { 267 268 [CmdletBinding()] 269 [Alias()] 270 [OutputType([int])] 271 Param 272 ( 273 # Param1 help description 274 [Parameter(Mandatory=$true, 275 ValueFromPipelineByPropertyName=$true, 276 Position=0)] 277 [string]$UserName, 278 [string]$ProfilePath, 279 [string]$domain = 'PHCORP' 280 ) 281 $methodname = 'userenvDP' 282 $script:nativeMethods = @(); 283 284 if (-not ([System.Management.Automation.PSTypeName]"$methodname.profile").Type) 285 { 286 add-type @" 287 using System.Runtime.InteropServices; 288 289 namespace $typename 290 { 291 public static class UserEnv 292 { 293 [DllImport("userenv.dll", CharSet = CharSet.Unicode, ExactSpelling = false, SetLastError = true)] 294 public static extern bool DeleteProfile(string sidString, string profilePath, string computerName); 295 296 [DllImport("kernel32.dll")] 297 public static extern uint GetLastError(); 298 } 299 300 public static class Profile 301 { 302 public static uint Delete(string sidString) 303 { //Profile path and computer name are optional 304 if (!UserEnv.DeleteProfile(sidString, null, null)) 305 { 306 return UserEnv.GetLastError(); 307 } 308 309 return 0; 310 } 311 } 312 } 313 "@ 314 } 315 316 #$SID= ((get-aduser -id $UserName -ErrorAction Stop).sid.value) 317 if($domain) 318 { 319 $objUser = New-Object System.Security.Principal.NTAccount($domain, $UserName) 320 $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 321 $SID = $strSID.Value 322 } 323 else 324 { 325 $objUser = New-Object System.Security.Principal.NTAccount($UserName) 326 $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 327 $SID = $strSID.Value 328 } 329 Write-Verbose "$UserName SID: $SID" 330 try 331 { 332 #http://stackoverflow.com/questions/31949002/c-sharp-delete-user-profile 333 $result = [userenvDP.Profile]::Delete($SID) 334 } 335 catch 336 { 337 Write-Error $_.Exception.Message; 338 break; 339 } 340 $LastError 341 } 342 343 Export-ModuleMember Create-NewProfile