0

我想使用 PowerShell 命令将我的 CentOS 机器添加到我的 Windows 域(在 centos 服务器上安装 PowerShell 之后)。

我可以使用带有领域的 linux 本机命令来做到这一点,但我不想要那种方法。

我有使用 unix 命令的解决方案,但由于安装了 powershell,我需要一个适用于 unix 和 windows 的命令。

4

1 回答 1

2

这是一个快速示例,显示了if elseif else运行特定于操作系统的功能以加入域的简单测试...

我使用了Join-LinuxToAD来自您的链接(所以它未经我测试),您需要适应Join-WindowsToAD您的特定域/安全需求。

function Join-LinuxToAD {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$DomainName,
        [Parameter(Mandatory = $true)]
        [string]$UserName 
    )
    #Is this host Linux?
    if (!$IsLinux) {Write-Error -Message 'This host is not Linux. Exiting'; exit}

    #Ensure you can lookup AD DNS
    nslookup $DomainName | Out-Null
    if ($LASTEXITCODE -ne 0) {Write-Error -Message 'Could not find domain in DNS. Checking settings'; exit}

    #Ensure Samba and dependencies installed
    yum install sssd realmd oddjob oddjob-mkhomedir adcli samba-common samba-common-tools krb5-workstation openldap-clients policycoreutils-python -y | Out-Null
    if ($LASTEXITCODE -ne 0) {Write-Error -Message 'Could not install one or more dependencies'; exit}

    #Join domain with realm
    realm join $DomainName --user=$UserName
    if ($LASTEXITCODE -ne 0) {Write-Error -Message "Could not join domain $DomainName. See error output"; exit}
    if ($LASTEXITCODE -eq 0) {Write-Output 'Yay! Your host is joined!'}
}

function Join-WindowsToAD {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$DomainName,
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$Credential
    )
    #Is this host Windows?
    if (!$IsWindows) {Write-Error -Message 'This host is not Windows. Exiting'; exit}

    Add-Computer -DomainName $DomainName -Credential $Credential
}

if ($IsLinux) {Join-LinuxToAD}
elseif ($IsWindows) {Join-WindowsToAD}
else { Write-Error -Message 'Unknown OS Type' ; exit}
于 2018-02-26T15:11:22.713 回答