Foreach is not processing each item, tries to process all as one lineHow do I copy items from list to list without foreach?I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?How to process a file in PowerShell line-by-line as a streamIs if(items != null) superfluous before foreach(T item in items)?ForEach Item in variable - get it's valuePowershell - Foreach Statement processingpowershell, foreach, get the number of lineOutput ALL results at the end of foreach instead of during each runforeach powershell loop open itemsforeach-object if return , returning all value issue

What is the relationship between spectral sequences and obstruction theory?

Don’t seats that recline flat defeat the purpose of having seatbelts?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Meaning of Bloch representation

What route did the Hindenburg take when traveling from Germany to the U.S.?

Rivers without rain

Critique of timeline aesthetic

Combinable filters

What do the phrase "Reeyan's seacrest" and the word "fraggle" mean in a sketch?

Why does processed meat contain preservatives, while canned fish needs not?

What is the difference between `command a[bc]d` and `command `ab,cd`

A ​Note ​on ​N!

Why other Westeros houses don't use wildfire?

Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?

Who is the Umpire in this picture?

Repelling Blast: Must targets always be pushed back?

Unexpected email from Yorkshire Bank

What was the first Intel x86 processor with "Base + Index * Scale + Displacement" addressing mode?

What's the polite way to say "I need to urinate"?

simple conditions equation

How would one muzzle a full grown polar bear in the 13th century?

How to stop co-workers from teasing me because I know Russian?

Will a top journal at least read my introduction?

Controversial area of mathematics



Foreach is not processing each item, tries to process all as one line


How do I copy items from list to list without foreach?I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?How to process a file in PowerShell line-by-line as a streamIs if(items != null) superfluous before foreach(T item in items)?ForEach Item in variable - get it's valuePowershell - Foreach Statement processingpowershell, foreach, get the number of lineOutput ALL results at the end of foreach instead of during each runforeach powershell loop open itemsforeach-object if return , returning all value issue






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I am building a script to pull the mac addresses of a computer, and convert them to GUIDs that are then queried to AD to find a match and retrieve the hostname associate with that guid.



Everything I've read about "Foreach" suggests that the code I am using "SHOULD" work.



$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration | select MacAddress
$NICMacs = $NIC.MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMacs -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
$MactoGUID = $MactoGUID -replace " ", ''
$NBG = [GUID]$MactoGUID
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


This should process each mac address found, stripping away the: characters and prepending 20 '0s' then convert to GUID format and query AD for a match.
Instead, it takes all the mac addresses found, concatenates them to one line and tries to process with all those numbers. Of course AD rejects it as an incorrect GUID.
If I use the same code with only 1 mac address, it is correctly formatted.










share|improve this question
























  • Please tag your question with the programming language you are using

    – Andrew Fan
    Mar 22 at 17:55






  • 6





    Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

    – RobV
    Mar 22 at 17:57












  • as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

    – Lee_Dailey
    Mar 22 at 20:02












  • Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

    – James Wallin
    Mar 24 at 0:01

















2















I am building a script to pull the mac addresses of a computer, and convert them to GUIDs that are then queried to AD to find a match and retrieve the hostname associate with that guid.



Everything I've read about "Foreach" suggests that the code I am using "SHOULD" work.



$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration | select MacAddress
$NICMacs = $NIC.MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMacs -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
$MactoGUID = $MactoGUID -replace " ", ''
$NBG = [GUID]$MactoGUID
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


This should process each mac address found, stripping away the: characters and prepending 20 '0s' then convert to GUID format and query AD for a match.
Instead, it takes all the mac addresses found, concatenates them to one line and tries to process with all those numbers. Of course AD rejects it as an incorrect GUID.
If I use the same code with only 1 mac address, it is correctly formatted.










share|improve this question
























  • Please tag your question with the programming language you are using

    – Andrew Fan
    Mar 22 at 17:55






  • 6





    Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

    – RobV
    Mar 22 at 17:57












  • as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

    – Lee_Dailey
    Mar 22 at 20:02












  • Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

    – James Wallin
    Mar 24 at 0:01













2












2








2








I am building a script to pull the mac addresses of a computer, and convert them to GUIDs that are then queried to AD to find a match and retrieve the hostname associate with that guid.



Everything I've read about "Foreach" suggests that the code I am using "SHOULD" work.



$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration | select MacAddress
$NICMacs = $NIC.MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMacs -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
$MactoGUID = $MactoGUID -replace " ", ''
$NBG = [GUID]$MactoGUID
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


This should process each mac address found, stripping away the: characters and prepending 20 '0s' then convert to GUID format and query AD for a match.
Instead, it takes all the mac addresses found, concatenates them to one line and tries to process with all those numbers. Of course AD rejects it as an incorrect GUID.
If I use the same code with only 1 mac address, it is correctly formatted.










share|improve this question
















I am building a script to pull the mac addresses of a computer, and convert them to GUIDs that are then queried to AD to find a match and retrieve the hostname associate with that guid.



Everything I've read about "Foreach" suggests that the code I am using "SHOULD" work.



$NIC = Get-WMIObject Win32_NetworkAdapterConfiguration | select MacAddress
$NICMacs = $NIC.MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMacs -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
$MactoGUID = $MactoGUID -replace " ", ''
$NBG = [GUID]$MactoGUID
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


This should process each mac address found, stripping away the: characters and prepending 20 '0s' then convert to GUID format and query AD for a match.
Instead, it takes all the mac addresses found, concatenates them to one line and tries to process with all those numbers. Of course AD rejects it as an incorrect GUID.
If I use the same code with only 1 mac address, it is correctly formatted.







powershell foreach






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 18:18









Sterling Archer

16.1k136291




16.1k136291










asked Mar 22 at 17:54









James WallinJames Wallin

133




133












  • Please tag your question with the programming language you are using

    – Andrew Fan
    Mar 22 at 17:55






  • 6





    Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

    – RobV
    Mar 22 at 17:57












  • as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

    – Lee_Dailey
    Mar 22 at 20:02












  • Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

    – James Wallin
    Mar 24 at 0:01

















  • Please tag your question with the programming language you are using

    – Andrew Fan
    Mar 22 at 17:55






  • 6





    Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

    – RobV
    Mar 22 at 17:57












  • as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

    – Lee_Dailey
    Mar 22 at 20:02












  • Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

    – James Wallin
    Mar 24 at 0:01
















Please tag your question with the programming language you are using

– Andrew Fan
Mar 22 at 17:55





Please tag your question with the programming language you are using

– Andrew Fan
Mar 22 at 17:55




6




6





Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

– RobV
Mar 22 at 17:57






Looks like a simple typo, inside the loop you should be using the $NICMac variable but you've used the original $NICMacs variable instead

– RobV
Mar 22 at 17:57














as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

– Lee_Dailey
Mar 22 at 20:02






as others have mentioned, you used the wrong $Var in the loop. this $MacString = $NICMacs -replace ":", "" uses the collection $Var instead of the current item $Var. [grin] ///// this is one reason you should ALWAYS try to use vividly different names for the collection and the current item. in your case, a better pair of names would be Foreach ($NL_Item in $NICMacList).

– Lee_Dailey
Mar 22 at 20:02














Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

– James Wallin
Mar 24 at 0:01





Sorry, new to posting here, it was powershell :) I did try referencing $NICMac in the foreach, but all it returned was the 20 0's

– James Wallin
Mar 24 at 0:01












2 Answers
2






active

oldest

votes


















0














Some of your steps can be combined and thus save intermediate vars using



  • Select-Object -ExpandProperty MacAddress


  • .PadLeft() method

As @RobV already pointed out in his comment usage of the wrong var caused the malfunction.



Foreach ($NICMac in (Get-WMIObject Win32_NetworkAdapterConfiguration |
Where-Object MacAddress -ne '' |
Select-Object -ExpandProperty MacAddress) )
$NBG = [GUID]($NICMac -replace ':').PadLeft(32,'0')
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' `
-Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey



I don't consider usage of a line continuation character esoteric,

here it allows keeping the overview.



I agree that Natural Line Continuations in PowerShell are preferable.






share|improve this answer

























  • Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

    – TheIncorrigible1
    Mar 22 at 19:02












  • Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

    – James Wallin
    Mar 23 at 23:55


















0














A couple items here:



  1. In your code, in line 7, you reference $NICMacs, but you should be referencing $NICMac since the foreach is running through the logic separately, not as a batch.

  2. You could simplify at least the top two lines into a single line without a pipe.

$NICMacs = (Get-WMIObject Win32_NetworkAdapterConfiguration).MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMac -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
[GUID]$NBG = $MactoGUID -replace " ", ''
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


Edit: in my suggested code, I think making $NBG should work with the [GUID] tag in front but I've never tried it






share|improve this answer

























  • I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

    – James Wallin
    Mar 24 at 0:00











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305312%2fforeach-is-not-processing-each-item-tries-to-process-all-as-one-line%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Some of your steps can be combined and thus save intermediate vars using



  • Select-Object -ExpandProperty MacAddress


  • .PadLeft() method

As @RobV already pointed out in his comment usage of the wrong var caused the malfunction.



Foreach ($NICMac in (Get-WMIObject Win32_NetworkAdapterConfiguration |
Where-Object MacAddress -ne '' |
Select-Object -ExpandProperty MacAddress) )
$NBG = [GUID]($NICMac -replace ':').PadLeft(32,'0')
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' `
-Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey



I don't consider usage of a line continuation character esoteric,

here it allows keeping the overview.



I agree that Natural Line Continuations in PowerShell are preferable.






share|improve this answer

























  • Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

    – TheIncorrigible1
    Mar 22 at 19:02












  • Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

    – James Wallin
    Mar 23 at 23:55















0














Some of your steps can be combined and thus save intermediate vars using



  • Select-Object -ExpandProperty MacAddress


  • .PadLeft() method

As @RobV already pointed out in his comment usage of the wrong var caused the malfunction.



Foreach ($NICMac in (Get-WMIObject Win32_NetworkAdapterConfiguration |
Where-Object MacAddress -ne '' |
Select-Object -ExpandProperty MacAddress) )
$NBG = [GUID]($NICMac -replace ':').PadLeft(32,'0')
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' `
-Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey



I don't consider usage of a line continuation character esoteric,

here it allows keeping the overview.



I agree that Natural Line Continuations in PowerShell are preferable.






share|improve this answer

























  • Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

    – TheIncorrigible1
    Mar 22 at 19:02












  • Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

    – James Wallin
    Mar 23 at 23:55













0












0








0







Some of your steps can be combined and thus save intermediate vars using



  • Select-Object -ExpandProperty MacAddress


  • .PadLeft() method

As @RobV already pointed out in his comment usage of the wrong var caused the malfunction.



Foreach ($NICMac in (Get-WMIObject Win32_NetworkAdapterConfiguration |
Where-Object MacAddress -ne '' |
Select-Object -ExpandProperty MacAddress) )
$NBG = [GUID]($NICMac -replace ':').PadLeft(32,'0')
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' `
-Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey



I don't consider usage of a line continuation character esoteric,

here it allows keeping the overview.



I agree that Natural Line Continuations in PowerShell are preferable.






share|improve this answer















Some of your steps can be combined and thus save intermediate vars using



  • Select-Object -ExpandProperty MacAddress


  • .PadLeft() method

As @RobV already pointed out in his comment usage of the wrong var caused the malfunction.



Foreach ($NICMac in (Get-WMIObject Win32_NetworkAdapterConfiguration |
Where-Object MacAddress -ne '' |
Select-Object -ExpandProperty MacAddress) )
$NBG = [GUID]($NICMac -replace ':').PadLeft(32,'0')
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' `
-Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey



I don't consider usage of a line continuation character esoteric,

here it allows keeping the overview.



I agree that Natural Line Continuations in PowerShell are preferable.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 22 at 20:25

























answered Mar 22 at 18:54









LotPingsLotPings

21k61633




21k61633












  • Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

    – TheIncorrigible1
    Mar 22 at 19:02












  • Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

    – James Wallin
    Mar 23 at 23:55

















  • Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

    – TheIncorrigible1
    Mar 22 at 19:02












  • Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

    – James Wallin
    Mar 23 at 23:55
















Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

– TheIncorrigible1
Mar 22 at 19:02






Instead of using esoteric line continuations for a long parameterset, I'd suggest utilizing splatting with a small footnote if you think the asker wouldn't understand it. Additionally, you don't explain why what the OP is doing isn't working.

– TheIncorrigible1
Mar 22 at 19:02














Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

– James Wallin
Mar 23 at 23:55





Thanks @RobV! I'll test it out at work on Monday, but on my home comp (not in AD) the foreach worked as I had hoped and gave a positive result in $NBG

– James Wallin
Mar 23 at 23:55













0














A couple items here:



  1. In your code, in line 7, you reference $NICMacs, but you should be referencing $NICMac since the foreach is running through the logic separately, not as a batch.

  2. You could simplify at least the top two lines into a single line without a pipe.

$NICMacs = (Get-WMIObject Win32_NetworkAdapterConfiguration).MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMac -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
[GUID]$NBG = $MactoGUID -replace " ", ''
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


Edit: in my suggested code, I think making $NBG should work with the [GUID] tag in front but I've never tried it






share|improve this answer

























  • I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

    – James Wallin
    Mar 24 at 0:00















0














A couple items here:



  1. In your code, in line 7, you reference $NICMacs, but you should be referencing $NICMac since the foreach is running through the logic separately, not as a batch.

  2. You could simplify at least the top two lines into a single line without a pipe.

$NICMacs = (Get-WMIObject Win32_NetworkAdapterConfiguration).MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMac -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
[GUID]$NBG = $MactoGUID -replace " ", ''
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


Edit: in my suggested code, I think making $NBG should work with the [GUID] tag in front but I've never tried it






share|improve this answer

























  • I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

    – James Wallin
    Mar 24 at 0:00













0












0








0







A couple items here:



  1. In your code, in line 7, you reference $NICMacs, but you should be referencing $NICMac since the foreach is running through the logic separately, not as a batch.

  2. You could simplify at least the top two lines into a single line without a pipe.

$NICMacs = (Get-WMIObject Win32_NetworkAdapterConfiguration).MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMac -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
[GUID]$NBG = $MactoGUID -replace " ", ''
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


Edit: in my suggested code, I think making $NBG should work with the [GUID] tag in front but I've never tried it






share|improve this answer















A couple items here:



  1. In your code, in line 7, you reference $NICMacs, but you should be referencing $NICMac since the foreach is running through the logic separately, not as a batch.

  2. You could simplify at least the top two lines into a single line without a pipe.

$NICMacs = (Get-WMIObject Win32_NetworkAdapterConfiguration).MacAddress

Foreach ($NICMac in $NICMacs)
{

$MacString = $NICMac -replace ":", ""
$MactoGUID = "00000000000000000000" + $MacString
[GUID]$NBG = $MactoGUID -replace " ", ''
$CompDetails = Get-ADComputer -Filter 'netbootGUID -like $NBG' -Properties netBootGUID -Server our.AD.server.ca -Credential $sessionKey


Edit: in my suggested code, I think making $NBG should work with the [GUID] tag in front but I've never tried it







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 4:07

























answered Mar 23 at 3:51









ShaneShane

11




11












  • I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

    – James Wallin
    Mar 24 at 0:00

















  • I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

    – James Wallin
    Mar 24 at 0:00
















I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

– James Wallin
Mar 24 at 0:00





I just get the added 0's with this implementation, which is what I was getting previously when I tried everyones suggestion of referencing $NICMac in the foreach statement

– James Wallin
Mar 24 at 0:00

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55305312%2fforeach-is-not-processing-each-item-tries-to-process-all-as-one-line%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript