?
Solved

Batch command to get FQDN of COMPUTER ala %userdnsdomain%

Posted on 2014-03-27
12
Medium Priority
?
2,690 Views
Last Modified: 2014-03-28
Hello,

I have the following batch file that calls up powershell, it works wonderfully. I now need to modify it to grab the computer FQDN instead of the user FDQN in the -Domain parameter.

How can I achieve this?

Powershell.exe -executionpolicy unrestricted -File C:\temp\gpo\source\2\import_gpos.ps1 -SomInfo -Domain %USERDNSDOMAIN% -backupfolder "C:\temp\gpo\source\2"

Open in new window

0
Comment
Question by:levertm
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39960205
(gwmi WIN32_ComputerSystem).Domain

or

$computer = Get-WmiObject -Class Win32_ComputerSystem


member .Domain

$computer.Domain
0
 

Author Comment

by:levertm
ID: 39960209
Hi again craig!

I know of (gwmi WIN32_ComputerSystem).Domain and use it in my other powershell scripts but how can I insert that into my batch file? batch wont recognize it.
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39960211
$objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()

$computerFQDN="{0}.{1}" -f $objIPProperties.HostName, $objIPProperties.DomainName

Powershell.exe -executionpolicy unrestricted -File C:\temp\gpo\source\2\import_gpos.ps1 -SomInfo -Domain $computerFQDN -backupfolder "C:\temp\gpo\source\2"
0
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39960228
$command=Your powershell cmdlets here"
powershell $command > myTmpFile
set /p PcFQDN= < myTmpFile
del /q myTmpFile

echo %PcFQDN%
0
 

Author Comment

by:levertm
ID: 39960233
Hi Wizard,

That's giving me errors:

$objlproperties is not recognized as an internal or external command, operable program or batch file.

$ComputerFQDN is not recognized as an internal or external command, operable program or batch file.
0
 
LVL 29

Expert Comment

by:becraig
ID: 39960247
for /f "tokens=*" %%a in ('powershell "(gwmi WIN32_ComputerSystem).Domain"') do set var=%%a
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39960257
Try this:

powershell -command "(gwmi WIN32_ComputerSystem).Domain" > myTmpFile
set /p PcFQDN= < myTmpFile
del /q myTmpFile

echo %PcFQDN%
0
 
LVL 41

Expert Comment

by:footech
ID: 39960277
Just modify your batch file to include this line
set FQDN=%computername%.%userdnsdomain%

Then your next line would be
Powershell.exe -executionpolicy unrestricted -File C:\temp\gpo\source\2\import_gpos.ps1 -SomInfo -Domain %FQDN% -backupfolder "C:\temp\gpo\source\2"
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 39960289
Foo I think the problem is he does not want the user domain.

What I gave him would work perfectly

for /f "tokens=*" %%a in ('powershell "(gwmi WIN32_ComputerSystem).Domain"') do set  FQDN=%%a

Open in new window

0
 
LVL 41

Expert Comment

by:footech
ID: 39960332
Perhaps you're right.  I interpreted the question to mean that he wanted the FQDN of the computer (i.e. something like "computername.domain.com").
0
 
LVL 29

Expert Comment

by:becraig
ID: 39960344
That is what he is after but based on his comment:

I now need to modify it to grab the computer FQDN instead of the user FDQN in the -Domain parameter.

I think he might have an environment where user and computer accounts are in separate domains, I might be wrong but that is the impression I get.
0
 

Author Comment

by:levertm
ID: 39961496
You're right becraig, my user and computer are in separate domains.

Your solution worked, thank you,
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question