dotnet core 报错 预定义的类型“System.Object”未定义或未导入

博主在使用VS Code进行.NET Core项目开发时,安装.NET Core 3.0预览版后,.NET Core 2.2解决方案下的项目报错。在百度无果后,推测是预览版问题。不卸载3.0版本,通过指定项目使用的SDK,回退到2.2.104版本,解决了项目报错问题。

使用vs code 在.net core 2.2下,  写一个项目玩,  偶然发现.net core 3.0 有preview版, 然后就装了一个试试,  然后以前.net core2.2 解决方案下的所有的项目都炸了, 每个类都报错    预定义的类型“System.Object”未定义或未导入

 

百度不到解决方法后, 想到可能是.net core 3.0 这个预览版有问题, 因为dotnet 默认使用最新的版本号, 再不卸载.net core 3.0 的情况下, 尝试指定项目使用的sdk,  我这边将回退到之前的 2.2.104 (可以使用 dotnet --list-sdks 查看已安装的.net sdk的所有版本, dotnet --version 查看当前使用的版本)

将控制台目录切换到自己项目根目录,执行

dotnet new global.json --sdk-version 2.2.104

 会在当前项目下生成一个gloabl.json

{
  "sdk": {
    "version": "2.2.104"
  }
}

应该是指定当前项目使用.net core 2.2.104, 但是其他项目还是报错, 之后再将所有的类库项目就指定使用.netcore2.2之后, 就不会报错了(复制生成的gloabl.json到每个项目的第一层目录)

PS C:\Users\Administrator\Desktop> # ===== 1. 修复网络连接问题 ===== PS C:\Users\Administrator\Desktop> function Repair-NetworkConnection { >> param( >> [string]$TargetIP = "192.168.1.100" >> ) >> >> # 手动设置DNS服务器 >> Write-Host "🔧 手动设置DNS服务器..." -ForegroundColor Cyan >> $adapters = Get-DnsClientServerAddress -AddressFamily IPv4 | >> Where-Object { $_.ServerAddresses -ne $null } >> $adapters | Set-DnsClientServerAddress -ServerAddresses ("8.8.8.8", "114.114.114.114") >> >> # 重启网络适配器(跳过物理适配器) >> Write-Host "🔄 重启虚拟网络适配器..." -ForegroundColor Cyan >> Get-NetAdapter | Where-Object { $_.InterfaceType -ne 6 } | Restart-NetAdapter -Confirm:$false >> >> # 清除网络缓存 >> Write-Host "🧹 清除网络缓存..." -ForegroundColor Cyan >> arp -d * >> ipconfig /flushdns >> nbtstat -R >> >> # 重置Winsock(不要求重启) >> Write-Host "⚙️ 重置Winsock..." -ForegroundColor Cyan >> netsh winsock reset catalog >> >> # 测试连接 >> if (Test-Connection $TargetIP -Count 1 -Quiet) { >> Write-Host "✅ 网络连接已恢复" -ForegroundColor Green >> return $true >> } >> >> Write-Host "⚠️ 网络连接部分恢复,可能需要系统重启" -ForegroundColor Yellow >> return $false >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 2. 修复Python环境(使用备用源)===== PS C:\Users\Administrator\Desktop> function Repair-PythonEnvironment { >> param( >> [string]$PythonPath = "E:\Python310" >> ) >> >> # 验证Python安装 >> if (-not (Test-Path $PythonPath)) { >> Write-Host "❌ Python路径不存在: $PythonPath" -ForegroundColor Red >> return $false >> } >> >> # 修复无效的包分布警告 >> $invalidDistPath = Join-Path $PythonPath "Lib\site-packages\-odelscope" >> if (Test-Path $invalidDistPath) { >> Remove-Item $invalidDistPath -Recurse -Force -ErrorAction SilentlyContinue >> Write-Host "✅ 已删除无效包分布: $invalidDistPath" -ForegroundColor Green >> } >> >> # 修复pip安装 >> try { >> Write-Host "🔧 修复pip安装..." -ForegroundColor Cyan >> >> # 使用国内镜像源 >> $getPipPath = Join-Path $env:TEMP "get-pip.py" >> $pipSources = @( >> "https://repo.huaweicloud.com/python/get-pip.py", >> "https://mirrors.aliyun.com/pypi/get-pip.py" >> ) >> >> foreach ($source in $pipSources) { >> try { >> Invoke-WebRequest -Uri $source -OutFile $getPipPath -ErrorAction Stop >> break >> } >> catch { >> Write-Host "⚠️ 下载失败: $source" -ForegroundColor Yellow >> } >> } >> >> if (-not (Test-Path $getPipPath)) { >> throw "所有镜像源均不可用" >> } >> >> # 重新安装pip >> python $getPipPath --no-warn-script-location >> >> # 配置国内镜像源 >> $pipConfig = @" >> [global] >> index-url = https://pypi.tuna.tsinghua.edu.cn/simple >> trusted-host = pypi.tuna.tsinghua.edu.cn >> "@ >> $pipConfig | Out-File "$env:APPDATA\pip\pip.ini" -Encoding ascii >> >> # 升级核心组件 >> python -m pip install --upgrade pip setuptools wheel >> >> Write-Host "✅ pip已修复" -ForegroundColor Green >> return $true >> } >> catch { >> Write-Host "❌ pip修复失败: $_" -ForegroundColor Red >> return $false >> } >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 3. 安装.NET SDK(兼容版本)===== PS C:\Users\Administrator\Desktop> function Install-DotNetSDK { >> # 获取最新LTS版本 >> $version = (Invoke-RestMethod "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/releases-index.json"). >> 'releases-index' | >> Where-Object { $_.'channel-version' -match '^\d+\.\d+$' } | >> Sort-Object { [version]$_.'channel-version' } -Descending | >> Select-Object -First 1 | >> ForEach-Object { >> (Invoke-RestMethod $_.'releases.json'). >> 'releases' | >> Where-Object { $_.'release-version' -match '^\d+\.\d+\.\d+$' } | >> Sort-Object { [version]$_.'release-version' } -Descending | >> Select-Object -First 1 >> } | >> ForEach-Object { $_.'release-version' } >> >> if (-not $version) { >> $version = "6.0.400" # 回退版本 >> } >> >> # 安装最新LTS版本 >> $installScriptPath = "$env:TEMP\dotnet-install.ps1" >> Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile $installScriptPath >> & $installScriptPath -Channel LTS -Runtime dotnet >> >> # 验证安装 >> $dotnetVersion = dotnet --version >> if ($dotnetVersion) { >> Write-Host "✅ .NET SDK $dotnetVersion 安装成功" -ForegroundColor Green >> return $true >> } >> >> Write-Host "❌ .NET SDK 安装失败" -ForegroundColor Red >> return $false >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 4. 定义缺失的关键函数 ===== PS C:\Users\Administrator\Desktop> function Install-FromRepository { >> param( >> [Parameter(Mandatory=$true)] >> [string]$PackageName, >> [string]$Version = "latest", >> [string]$RepositoryPath = "E:\ai_pip_repository" >> ) >> >> $downloadedDir = Join-Path $RepositoryPath "downloaded_packages" >> >> # 搜索本地仓库 >> $localPackages = Get-ChildItem -Path $downloadedDir -Recurse -ErrorAction SilentlyContinue | >> Where-Object { $_.Name -like "*$PackageName*" -and $_.Extension -in @('.whl', '.gz', '.zip') } >> >> if ($localPackages) { >> # 版本选择逻辑 >> if ($Version -eq "latest") { >> $selectedPackage = $localPackages | >> Sort-Object { [regex]::Match($_.Name, '(\d+\.)+\d+').Value } -Descending | >> Select-Object -First 1 >> } else { >> $selectedPackage = $localPackages | >> Where-Object { $_.Name -match "$PackageName-$Version" } | >> Select-Object -First 1 >> } >> >> if (-not $selectedPackage) { >> Write-Host "⚠️ 仓库中找到指定版本: $PackageName==$Version" -ForegroundColor Yellow >> return >> } >> >> Write-Host "🚀 使用仓库中的版本: $($selectedPackage.Name)" -ForegroundColor Yellow >> >> # 安装主包 >> python -m pip install $selectedPackage.FullName --no-deps --no-index >> >> # 安装依赖 >> $depReport = Test-RepositoryDependency -PackageName $PackageName -RepositoryPath $RepositoryPath >> if ($depReport.MissingDependencies.Count -gt 0) { >> Write-Host "🔍 安装依赖包..." -ForegroundColor Cyan >> $depReport.MissingDependencies | ForEach-Object { >> Install-FromRepository $_ -RepositoryPath $RepositoryPath >> } >> } >> return >> } >> >> # 本地仓库不存在则下载并保存 >> Write-Host "🌐 从镜像下载: $PackageName" -ForegroundColor Magenta >> >> # 创建临时下载目录 >> $tempDir = Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString()) >> New-Item -ItemType Directory -Path $tempDir -Force | Out-Null >> >> try { >> # 下载包 >> if ($Version -eq "latest") { >> python -m pip download $PackageName -d $tempDir >> } else { >> python -m pip download "${PackageName}==${Version}" -d $tempDir >> } >> >> # 获取下载的文件 >> $downloadedFiles = Get-ChildItem $tempDir -File -ErrorAction SilentlyContinue | >> Where-Object { $_.Extension -in @('.whl', '.gz', '.zip') } >> >> if (-not $downloadedFiles) { >> throw "找到下载的包文件" >> } >> >> # 安装并保存每个包 >> foreach ($file in $downloadedFiles) { >> # 安装主包 >> python -m pip install $file.FullName >> >> # 保存到仓库 >> $savePath = Join-Path $downloadedDir $file.Name >> Copy-Item -Path $file.FullName -Destination $savePath -Force >> Write-Host "💾 已保存到仓库: $($file.Name)" -ForegroundColor Green >> } >> } >> catch { >> Write-Host "❌ 安装失败: $_" -ForegroundColor Red >> } >> finally { >> # 清理临时目录 >> Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue >> } >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> function Update-RepositoryIndex { >> param( >> [string]$RepositoryPath = "E:\ai_pip_repository" >> ) >> >> $downloadedDir = Join-Path $RepositoryPath "downloaded_packages" >> >> # 创建简单的索引文件 >> $indexFile = Join-Path $RepositoryPath "index.txt" >> Get-ChildItem $downloadedDir -File | >> Select-Object Name, Length, LastWriteTime | >> Format-Table -AutoSize | >> Out-File $indexFile -Encoding UTF8 >> >> Write-Host "✅ 仓库索引已更新: $indexFile" -ForegroundColor Green >> } PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # ===== 执行步骤 ===== PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 1. 修复网络连接 PS C:\Users\Administrator\Desktop> Repair-NetworkConnection -TargetIP "192.168.1.100" 🔧 手动设置DNS服务器... 🔄 重启虚拟网络适配器... 🧹 清除网络缓存... Windows IP 配置 已成功刷新 DNS 解析缓存。 NBT 远程缓存名称表的成功清除和预加载。 ⚙️ 重置Winsock... 成功地重置 Winsock 目录。 你必须重新启动计算机才能完成重置。 ⚠️ 网络连接部分恢复,可能需要系统重启 False PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 2. 修复Python环境 PS C:\Users\Administrator\Desktop> Repair-PythonEnvironment 🔧 修复pip安装... ⚠️ 下载失败: https://repo.huaweicloud.com/python/get-pip.py E:\Python310\lib\site-packages\_distutils_hack\__init__.py:15: UserWarning: Distutils was imported before Setuptools, but importing Setuptools also replaces the `distutils` module in `sys.modules`. This may lead to undesirable behaviors or errors. To avoid these issues, avoid using distutils directly, ensure that setuptools is installed in the traditional way (e.g. not an editable install), and/or make sure that setuptools is always imported before distutils. warnings.warn( E:\Python310\lib\site-packages\_distutils_hack\__init__.py:30: UserWarning: Setuptools is replacing distutils. Support for replacing an already imported distutils is deprecated. In the future, this condition will fail. Register concerns at https://github.com/pypa/setuptools/issues/new?template=distutils-deprecation.yml warnings.warn( Traceback (most recent call last): File "C:\Users\ADMINI~1\AppData\Local\Temp\get-pip.py", line 23974, in <module> main() File "C:\Users\ADMINI~1\AppData\Local\Temp\get-pip.py", line 199, in main bootstrap(tmpdir=tmpdir) File "C:\Users\ADMINI~1\AppData\Local\Temp\get-pip.py", line 121, in bootstrap import setuptools # noqa File "E:\Python310\lib\site-packages\setuptools\__init__.py", line 21, in <module> import _distutils_hack.override # noqa: F401 File "E:\Python310\lib\site-packages\_distutils_hack\override.py", line 1, in <module> __import__('_distutils_hack').do_override() File "E:\Python310\lib\site-packages\_distutils_hack\__init__.py", line 89, in do_override ensure_local_distutils() File "E:\Python310\lib\site-packages\_distutils_hack\__init__.py", line 76, in ensure_local_distutils assert '_distutils' in core.__file__, core.__file__ AssertionError: E:\Python310\lib\distutils\core.py ❌ pip修复失败: 能找到路径“C:\Users\Administrator\AppData\Roaming\pip\pip.ini”的一部分。 False PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 3. 安装.NET SDK PS C:\Users\Administrator\Desktop> Install-DotNetSDK dotnet-install: Remote file https://builds.dotnet.microsoft.com/dotnet/Runtime/8.0.19/dotnet-runtime-8.0.19-win-x64.zip size is 33298518 bytes. dotnet-install: Downloaded file https://builds.dotnet.microsoft.com/dotnet/Runtime/8.0.19/dotnet-runtime-8.0.19-win-x64.zip size is 33298518 bytes. dotnet-install: The remote and local file sizes are equal. dotnet-install: Extracting the archive. dotnet-install: Adding to current process PATH: "C:\Users\Administrator\AppData\Local\Microsoft\dotnet\". Note: This change will not be visible if PowerShell was run as a child process. dotnet-install: Note that the script does not ensure your Windows version is supported during the installation. dotnet-install: To check the list of supported versions, go to https://learn.microsoft.com/dotnet/core/install/windows#supported-versions dotnet-install: Installed version is 8.0.19 dotnet-install: Installation finished The command could not be loaded, possibly because: * You intended to execute a .NET application: The application '--version' does not exist. * You intended to execute a .NET SDK command: No .NET SDKs were found. Download a .NET SDK: https://aka.ms/dotnet/download Learn about SDK resolution: https://aka.ms/dotnet/sdk-not-found ❌ .NET SDK 安装失败 False PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 4. 初始化仓库 PS C:\Users\Administrator\Desktop> Initialize-PipRepository -RepositoryPath "E:\ai_pip_repository" Initialize-PipRepository : 无法将“Initialize-PipRepository”项识别为 cmdlet、函数、脚本文件可运行程序的名称。请检查 名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + Initialize-PipRepository -RepositoryPath "E:\ai_pip_repository" + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Initialize-PipRepository:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 5. 安装包 PS C:\Users\Administrator\Desktop> Install-FromRepository "torch" -Version "2.0.0" -RepositoryPath "E:\ai_pip_repository" 🌐 从镜像下载: torch E:\Python310\python.exe: No module named pip ❌ 安装失败: 找到下载的包文件 PS C:\Users\Administrator\Desktop> Install-FromRepository "torchvision" -Version "0.15.1" -RepositoryPath "E:\ai_pip_repository" 🌐 从镜像下载: torchvision E:\Python310\python.exe: No module named pip ❌ 安装失败: 找到下载的包文件 PS C:\Users\Administrator\Desktop> Install-FromRepository "torchaudio" -Version "2.0.1" -RepositoryPath "E:\ai_pip_repository" 🌐 从镜像下载: torchaudio E:\Python310\python.exe: No module named pip ❌ 安装失败: 找到下载的包文件 PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 6. 更新仓库索引 PS C:\Users\Administrator\Desktop> Update-RepositoryIndex -RepositoryPath "E:\ai_pip_repository" ✅ 仓库索引已更新: E:\ai_pip_repository\index.txt PS C:\Users\Administrator\Desktop>
最新发布
08-23
PS C:\Users\Administrator> PS C:\Users\Administrator> # 步骤1: 导航到项目目录 >> cd E:\ProjectEcosystem\ProjectMonitor >> >> # 步骤2: 创建新项目 >> .\Initialize-DevEnv.ps1 >> Initialize-Project -ProjectName "EcoMonitor" -ProjectType "node" >> >> # 步骤3: 启动项目监控 >> .\Monitor-Project.ps1 >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 E:\ProjectEcosystem\ProjectMonitor\Initialize-DevEnv.ps1:199 字符: 1 + Export-ModuleMember -Function Initialize-Project + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand Initialize-Project : 无法将“Initialize-Project”项识别为 cmdlet、函数、脚本文件可运行程序的名称。请检查名称的拼写, 如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:6 字符: 1 + Initialize-Project -ProjectName "EcoMonitor" -ProjectType "node" + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Initialize-Project:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS E:\ProjectEcosystem\ProjectMonitor> .\Initialize-DevEnv.ps1 >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 E:\ProjectEcosystem\ProjectMonitor\Initialize-DevEnv.ps1:199 字符: 1 + Export-ModuleMember -Function Initialize-Project + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand PS E:\ProjectEcosystem\ProjectMonitor> code .\MyWebApp\MyWebApp.code-workspace >> PS E:\ProjectEcosystem\ProjectMonitor> # 在项目初始化函数中添加 >> function Initialize-Project { >> # ... 其他代码 ... >> >> # 创建信任锚文件 >> $trustAnchorDir = Join-Path $env:APPDATA "Code/User/WorkspaceStorage" >> $trustAnchorFile = Join-Path $trustAnchorDir "trusted-workspaces.json" >> >> if (-not (Test-Path $trustAnchorFile)) { >> New-Item -Path $trustAnchorFile -Force | Out-Null >> Set-Content $trustAnchorFile '{"$mid":1,"folders":{}}' >> } >> >> # 添加当前项目到信任列表 >> $trusted = Get-Content $trustAnchorFile | ConvertFrom-Json >> $projectHash = (Get-FileHash -Algorithm SHA256 -InputStream ([IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($projectDir)))).Hash >> $trusted.folders | Add-Member -NotePropertyName $projectHash -NotePropertyValue @{ >> "trusted" = $true >> "uri" = [Uri]::new($projectDir).AbsoluteUri >> } -Force >> >> $trusted | ConvertTo-Json -Depth 10 | Set-Content $trustAnchorFile >> >> # ... 其他代码 ... >> } >> PS E:\ProjectEcosystem\ProjectMonitor> # 修改最后打开项目的命令 >> Write-Host "🛠 Open project in VS Code with:" -ForegroundColor Cyan >> Write-Host " code --trust '$ProjectName.code-workspace'" -ForegroundColor Yellow >> 🛠 Open project in VS Code with: code --trust '.code-workspace' PS E:\ProjectEcosystem\ProjectMonitor> # 打开项目时添加 --trust 参数 >> code --trust .\MyWebApp\MyWebApp.code-workspace >> Warning: 'trust' is not in the list of known options, but still passed to Electron/Chromium. PS E:\ProjectEcosystem\ProjectMonitor> %APPDATA%\Code\User\WorkspaceStorage\trusted-workspaces.json %APPDATA%\Code\User\WorkspaceStorage\trusted-workspaces.json : 无法加载模块“%APPDATA%”。有关详细信息,请运行“Import- Module %APPDATA%”。 所在位置 行:1 字符: 1 + %APPDATA%\Code\User\WorkspaceStorage\trusted-workspaces.json + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (%APPDATA%\Code\...workspaces.json:String) [], CommandNotFoundException + FullyQualifiedErrorId : CouldNotAutoLoadModule PS E:\ProjectEcosystem\ProjectMonitor> { >> "$mid": 1, >> "folders": { >> "SHA256_HASH_OF_PROJECT_PATH": { >> "trusted": true, >> "uri": "file:///path/to/project" >> } >> } >> } >> 所在位置 行:2 字符: 9 + "$mid": 1, + ~ 表达式语句中包含意外的标记“:”。 所在位置 行:4 字符: 34 + "SHA256_HASH_OF_PROJECT_PATH": { + ~ 表达式语句中包含意外的标记“:”。 所在位置 行:5 字符: 16 + "trusted": true, + ~ 表达式语句中包含意外的标记“:”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken PS E:\ProjectEcosystem\ProjectMonitor> function Initialize-Project { >> param( >> [Parameter(Mandatory=$true)] >> [string]$ProjectName, >> >> [ValidateSet("web", "python", "node", "dotnet")] >> [string]$ProjectType = "web", >> >> [string]$RootPath = "E:\ProjectEcosystem\ProjectMonitor" >> ) >> >> # ... 项目创建代码 ... >> >> # 信任锚处理 >> $trustAnchorDir = Join-Path $env:APPDATA "Code/User/WorkspaceStorage" >> New-Item -ItemType Directory -Path $trustAnchorDir -Force | Out-Null >> $trustAnchorFile = Join-Path $trustAnchorDir "trusted-workspaces.json" >> >> # 创建更新信任锚文件 >> $trusted = @{"`$mid"=1; "folders"=@{}} >> if (Test-Path $trustAnchorFile) { >> $trusted = Get-Content $trustAnchorFile | ConvertFrom-Json -AsHashtable >> } >> >> # 添加当前项目 >> $projectHash = (Get-FileHash -Algorithm SHA256 -InputStream ( >> [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($projectDir)) >> )).Hash >> >> $trusted.folders[$projectHash] = @{ >> "trusted" = $true >> "uri" = [Uri]::new($projectDir).AbsoluteUri >> } >> >> $trusted | ConvertTo-Json -Depth 10 | Set-Content $trustAnchorFile >> >> # 输出打开命令 >> Write-Host "✅ Project '$ProjectName' created at: $projectDir" -ForegroundColor Green >> Write-Host "🛠 Open project securely with:" -ForegroundColor Cyan >> Write-Host " code --trust '$ProjectName.code-workspace'" -ForegroundColor Yellow >> } >> PS E:\ProjectEcosystem\ProjectMonitor> # 为每个项目创建专用用户 >> $secureUser = "user_$ProjectName" >> New-LocalUser -Name $secureUser -Password (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) >> Name Enabled Description ---- ------- ----------- user_ True PS E:\ProjectEcosystem\ProjectMonitor> # 限制项目资源访问 >> New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa" ` >> -Name "DisableRestrictedAdmin" ` >> -Value 0 ` >> -PropertyType DWORD ` >> -Force >> DisableRestrictedAdmin : 0 PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control PSChildName : Lsa PSDrive : HKLM PSProvider : Microsoft.PowerShell.Core\Registry PS E:\ProjectEcosystem\ProjectMonitor> # 添加自动安全扫描 >> Start-Job -ScriptBlock { >> & "C:\Program Files\Windows Defender\MpCmdRun.exe" -Scan -ScanType 3 -File $using:projectDir >> } >> Start-Job : 无法检索 using 变量 '$using:projectDir' 的值,因为在本地会话中设置该变量。 所在位置 行:2 字符: 1 + Start-Job -ScriptBlock { + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Job],RuntimeException + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.StartJobCommand PS E:\ProjectEcosystem\ProjectMonitor>
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值