PowerShell脚本,可以用于自动加入或退出域:BAT批处理脚本,可以用于自动加入或退出域:

PowerShell中创建网页版的自动加入或退出域脚本,您可以使用PowerShell Web Access(PWA)功能。PWA允许您在Web浏览器中通过安全的HTTPS连接远程运行PowerShell命令,并可与Windows身份验证一起使用。

以下是一个示例脚本,可用于创建PWA网页版的自动加入或退出域:

powershellCopy Code
# 安装PWA组件
Install-WindowsFeature -Name "WindowsPowerShellWebAccess" -IncludeManagementTools

# 配置PWA
Set-PswaAuthorizationRule -UserName "domain_admin_username" -ComputerName "*" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "domain_user_username" -ComputerName "*" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "local_admin_username" -ComputerName "*" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "local_user_username" -ComputerName "*" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "domain_admin_username" -ComputerName "localhost" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "domain_user_username" -ComputerName "localhost" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "local_admin_username" -ComputerName "localhost" -ConfigurationName "Default" -Enabled $true
Set-PswaAuthorizationRule -UserName "local_user_username" -ComputerName "localhost" -ConfigurationName "Default" -Enabled $true

# 重启IIS服务
Restart-Service -Name "W3SVC"

# 创建PWA网页
$Html = @"
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>域操作</title>
</head>
<body>
    <h1>域操作</h1>
    <p>请选择要执行的操作:</p>
    <button onclick="JoinDomain()">加入域</button>
    <button onclick="LeaveDomain()">退出域</button>

    <script type="text/javascript">
        function JoinDomain() {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "/pswa/Default/Invoke", true);
            xhr.setRequestHeader("Content-type", "application/json");

            var command = {};
            command.Command = "Add-Computer -DomainName your_domain_name -Credential (New-Object System.Management.Automation.PSCredential('domain_admin_username', (ConvertTo-SecureString 'domain_admin_password' -AsPlainText -Force))) -Restart";
            command.UserName = "domain_admin_username";
            command.Password = "domain_admin_password";

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert("加入域成功!请重新启动计算机。");
                }
            };

            xhr.send(JSON.stringify(command));
        }

        function LeaveDomain() {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "/pswa/Default/Invoke", true);
            xhr.setRequestHeader("Content-type", "application/json");

            var command = {};
            command.Command = "Remove-Computer -Credential (New-Object System.Management.Automation.PSCredential('domain_admin_username', (ConvertTo-SecureString 'domain_admin_password' -AsPlainText -Force))) -Force -Restart";
            command.UserName = "domain_admin_username";
            command.Password = "domain_admin_password";

            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert("退出域成功!请重新启动计算机。");
                }
            };

            xhr.send(JSON.stringify(command));
        }
    </script>
</body>
</html>
"@

# 将HTML写入文件
$Html | Out-File -FilePath "C:\inetpub\wwwroot\index.html" -Encoding UTF8

# 显示文件路径
Write-Host "请在浏览器中打开以下网址:"
Write-Host "https://localhost/index.html"

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

运行上述脚本后,将会自动安装PWA组件并配置授权规则。然后将会在 "C:\inetpub\wwwroot" 目录下创建一个名为 "index.html" 的网页文件,并在控制台中显示该文件的访问URL。通过该URL即可在Web浏览器中访问该页面,并选择相应的操作以执行自动加入或退出域的任务。

请注意,在运行这个脚本之前,请确保已经安装了IIS,并谨慎操作,以免对系统造成意外影响。

使用PowerShell和Windows Forms创建简单界面的示例脚本,可用于自动加入或退出域:

powershellCopy Code
Add-Type -AssemblyName System.Windows.Forms

# 创建窗体
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "域操作"
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.FormBorderStyle = "FixedDialog"
$Form.StartPosition = "CenterScreen"

# 创建标签
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(20,20)
$Label.Size = New-Object System.Drawing.Size(200,20)
$Label.Text = "请选择要执行的操作:"
$Form.Controls.Add($Label)

# 创建加入域按钮
$JoinButton = New-Object System.Windows.Forms.Button
$JoinButton.Location = New-Object System.Drawing.Point(20,50)
$JoinButton.Size = New-Object System.Drawing.Size(120,30)
$JoinButton.Text = "加入域"
$JoinButton.Add_Click({
    $domain = "your_domain_name"
    $username = "domain_admin_username"
    $password = "domain_admin_password"

    $securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $securePassword

    Add-Computer -DomainName $domain -Credential $credential -Restart
})
$Form.Controls.Add($JoinButton)

# 创建退出域按钮
$LeaveButton = New-Object System.Windows.Forms.Button
$LeaveButton.Location = New-Object System.Drawing.Point(150,50)
$LeaveButton.Size = New-Object System.Drawing.Size(120,30)
$LeaveButton.Text = "退出域"
$LeaveButton.Add_Click({
    $username = "domain_admin_username"
    $password = "domain_admin_password"

    $securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $securePassword

    Remove-Computer -Credential $credential -Force -Restart
})
$Form.Controls.Add($LeaveButton)

# 显示窗体
$Form.ShowDialog()

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

运行上述脚本后,将显示一个具有两个按钮(加入域和退出域)的窗体。根据需要选择相应的操作,单击对应的按钮即可执行操作。

请注意,在运行这个脚本之前,请确保已经安装了PowerShell,并谨慎操作,以免对系统造成意外影响。

PowerShell脚本,可以用于自动加入或退出域:

自动加入域的脚本(auto_join_domain.ps1):

powershellCopy Code
$domain = "your_domain_name"
$username = "domain_admin_username"
$password = "domain_admin_password" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)

Write-Host "Joining the domain..."
Add-Computer -DomainName $domain -Credential $credential -Restart

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

自动退出域的脚本(auto_leave_domain.ps1):

powershellCopy Code
$username = "domain_admin_username"
$password = "domain_admin_password" | ConvertTo-SecureString -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username,$password)

Write-Host "Leaving the domain..."
Remove-Computer -Credential $credential -Force -Restart

将上述脚本中的 "domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

请注意,在运行这些脚本之前,请确保以管理员权限运行PowerShell脚本,并谨慎操作,以免对系统造成意外影响。

可以用于自动加入或退出域:

自动加入域的脚本(auto_join_domain.bat):

Copy Code
@echo off
set domain=your_domain_name
set username=domain_admin_username
set password=domain_admin_password

echo Joining the domain...
netdom join %computername% /domain:%domain% /userd:%username% /passwordd:%password%
if %errorlevel%==0 (
    echo Domain join successful.
    echo Rebooting the system...
    shutdown /r /t 0
) else (
    echo Domain join failed.
    pause
)

exit

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

自动退出域的脚本(auto_leave_domain.bat):

Copy Code
@echo off
set username=domain_admin_username
set password=domain_admin_password

echo Leaving the domain...
netdom remove %computername% /domain /userd:%username% /passwordd:%password% /force
if %errorlevel%==0 (
    echo Domain leave successful.
    echo Rebooting the system...
    shutdown /r /t 0
) else (
    echo Domain leave failed.
    pause
)

exit

将上述脚本中的 "domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

请注意,在运行这些脚本之前,请确保以管理员权限运行批处理文件,并谨慎操作,以免对系统造成意外影响。

 

VBS脚本,可以用于自动加入或退出域:

自动加入域的脚本(auto_join_domain.vbs):

vbscriptCopy Code
Dim objNetwork, strDomain, strUsername, strPassword

strDomain = "your_domain_name"
strUsername = "domain_admin_username"
strPassword = "domain_admin_password"

Set objNetwork = CreateObject("WScript.Network")

WScript.Echo "Joining the domain..."
objNetwork.AddWindowsDomain strDomain, strUsername, strPassword

If Err.Number = 0 Then
    WScript.Echo "Domain join successful."
    WScript.Echo "Rebooting the system..."
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "shutdown /r /t 0", 0, True
Else
    WScript.Echo "Domain join failed."
    WScript.Quit 1
End If

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

自动退出域的脚本(auto_leave_domain.vbs):

vbscriptCopy Code
Dim objNetwork, strUsername, strPassword

strUsername = "domain_admin_username"
strPassword = "domain_admin_password"

Set objNetwork = CreateObject("WScript.Network")

WScript.Echo "Leaving the domain..."
objNetwork.RemoveWindowsDomain strUsername, strPassword, 0

If Err.Number = 0 Then
    WScript.Echo "Domain leave successful."
    WScript.Echo "Rebooting the system..."
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "shutdown /r /t 0", 0, True
Else
    WScript.Echo "Domain leave failed."
    WScript.Quit 1
End If

将上述脚本中的 "domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

请注意,在运行这些脚本之前,请确保以管理员权限运行VBS脚本,并谨慎操作,以免对系统造成意外影响。

JavaScript(.js)通常用于在Web浏览器中编写脚本,而不是用于执行本地操作系统任务。因此,在JavaScript中直接加入或退出域可能会受到限制。但你可以使用Node.js来运行JavaScript脚本,并借助相应的模块来实现自动加入或退出域的功能。

以下是一个示例使用Node.js和node-powershell模块的JavaScript脚本,可以用于自动加入或退出域:

自动加入域的脚本(auto_join_domain.js):

javascriptCopy Code
const PowerShell = require('node-powershell');

const domain = 'your_domain_name';
const username = 'domain_admin_username';
const password = 'domain_admin_password';

const ps = new PowerShell();

ps.addCommand(`$password = ConvertTo-SecureString -String "${password}" -AsPlainText -Force;
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "${username}", $password;
    Add-Computer -DomainName "${domain}" -Credential $credential -Restart`);

ps.invoke()
    .then(output => {
        console.log('Join domain successful.');
        console.log(output);
    })
    .catch(err => {
        console.error('Failed to join domain.');
        console.error(err);
    })
    .finally(() => {
        ps.dispose();
    });

将上述脚本中的 "your_domain_name" 替换为实际的域名,"domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

自动退出域的脚本(auto_leave_domain.js):

javascriptCopy Code
const PowerShell = require('node-powershell');

const username = 'domain_admin_username';
const password = 'domain_admin_password';

const ps = new PowerShell();

ps.addCommand(`$password = ConvertTo-SecureString -String "${password}" -AsPlainText -Force;
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "${username}", $password;
    Remove-Computer -Credential $credential -Force -Restart`);

ps.invoke()
    .then(output => {
        console.log('Leave domain successful.');
        console.log(output);
    })
    .catch(err => {
        console.error('Failed to leave domain.');
        console.error(err);
    })
    .finally(() => {
        ps.dispose();
    });

将上述脚本中的 "domain_admin_username" 和 "domain_admin_password" 替换为域管理员的用户名和密码。

请注意,在运行这些脚本之前,请确保已经安装了Node.js和node-powershell模块,并谨慎操作,以免对系统造成意外影响。

posted @ 2024-05-20 10:43  suv789  阅读(236)  评论(0编辑  收藏  举报