2017-04-10 15 views
1

私はローカルユーザを作成してパスワードを設定するスクリプトです。ユーザ名とパスワードがNULLまたはEMPTYを返す

エラーをチェックして、名前とパスワードが空白でないことを確認しました。ユーザー名とパスワードが空白でない場合でも、何らかの理由でそれはまだnullまたは空でない場合、それはNULLまたは空であることを述べている:あなたは$usernameのためとする第二の中にチェックする必要が最初のif声明の中で

$Computername = $env:COMPUTERNAME 

$ADSIComp = [adsi]"WinNT://$Computername" 
$Username = 'TestProx' 
$Username = Read-Host -Prompt 'Please enter the New User' 
#check that Username is not empty 
if([string]::IsNullOrEmpty($destDir)) 
{    
    Write-Host "Username is NULL or EMPTY"    
} 
else 
{ 
    $NewUser = $ADSIComp.Create('User',$Username) 
    #Create password 

    $Password = Read-Host -Prompt "Enter password for $Username" -AsSecureString 
    #check that password is not empty 
    if([string]::IsNullOrEmpty($destDir)) 
    {    
     Write-Host "password is NULL or EMPTY"    
    } 
    else 
    {  
     $BSTR = [system.runtime.interopservices.marshal]::SecureStringToBSTR($Password)  
     $_password = [system.runtime.interopservices.marshal]::PtrToStringAuto($BSTR) 
     #Set password on account  
     $NewUser.SetPassword(($_password))  
     $NewUser.SetInfo() 
    } 
} 

答えて

2

の代わりに$destDir

$Computername = $env:COMPUTERNAME 

$ADSIComp = [adsi]"WinNT://$Computername" 
$Username = 'TestProx' 
$Username = Read-Host -Prompt 'Please enter the New User' 
#check that Username is not empty 
if([string]::IsNullOrEmpty($Username)) 
{    
    Write-Host "Username is NULL or EMPTY"    
} 
else 
{ 
    $NewUser = $ADSIComp.Create('User',$Username) 
    #Create password 

    $Password = Read-Host -Prompt "Enter password for $Username" -AsSecureString 
    #check that password is not empty 
    if([string]::IsNullOrEmpty($Password)) 
    {    
     Write-Host "password is NULL or EMPTY"    
    } 
    else 
    {  
     $BSTR = [system.runtime.interopservices.marshal]::SecureStringToBSTR($Password)  
     $_password = [system.runtime.interopservices.marshal]::PtrToStringAuto($BSTR) 
     #Set password on account  
     $NewUser.SetPassword(($_password))  
     $NewUser.SetInfo() 
    } 
} 
関連する問題