基于if语句打印的变量带有额外字符(Python)

时间:2016-09-29 14:03:27

标签: python

在下面的脚本中,我试图以某种方式显示设备名称,具体取决于它是否已加入域或是否在工作组中。

大部分脚本都有效,但我在devicename变量中获得了额外的字符。

打印输出如下:

print dev_name = hostname    
print fqdn_name = hostname + domain     
print devicename = ('dev_name or fqdn_name',) - extra characters are (' ',)
print devicename should = dev_name or fqdn_name

非常感谢任何帮助。

谢谢,

import clr

clr.AddReference('System.Management.Automation')

from System.Management.Automation import (
PSMethod, RunspaceInvoke
)
RUNSPACE = RunspaceInvoke()

import urllib
import urllib2
import traceback
import base64
import math
import ssl
import functools

def to_ascii(s):
    # ignore non-ascii chars
    return s.encode('ascii','ignore')

def wmi(query):
    return [dict([(prop.Name, prop.Value) for prop in psobj.Properties]) for psobj in RUNSPACE.Invoke(query)]

computer_system  = wmi('Get-WmiObject Win32_ComputerSystem -Namespace "root\CIMV2"')[0] # take first
dev_name         = to_ascii(computer_system.get('Name')).upper()
fqdn_name        = to_ascii(computer_system.get('Name')).upper() + '.' + to_ascii(computer_system.get('Domain')).lower()

devicename = ''
for dmn in ['domain1', 'domain2', 'domain3']:
if dmn == to_ascii(computer_system.get('Domain')).strip():
    devicename = fqdn_name,
    break
if dmn != to_ascii(computer_system.get('Domain')).strip():
    devicename = dev_name,
print devicename
print dev_name
print fqdn_name

0 个答案:

没有答案